Captain Code: Unleash Your Coding Superpower with Python by Shmuel Forta & Ben Forta
Author:Shmuel Forta & Ben Forta [Shmuel Forta]
Language: eng
Format: epub
Publisher: Addison-Wesley Professional
Published: 2021-12-24T16:00:00+00:00
Returning Values
Now you know how to create a function and how to pass it arguments. The last thing we need to look at is how to return values from a function.
Functions can return values. Think about how you used input(). It interacted with the user and then returned whatever the user typed as the result. upper() returns the uppercase version of a string. now() returns todayâs date and time.
Your own functions will frequently need to be able to return values, too, and you can do this with the (very conveniently named) return statement.
Letâs look at an exampleâand a very useful example at that. You know what this code does, right?
num=input("Enter a number: ")
This code asks the user to Enter a number: and then stores whatever they typed into a variable named num.
So, if the user typed abc (which is definitely not a number), that will be saved into variable num. Not good.
So, hereâs a better version of the code:
Click here to view code image
num=inputNumber("Enter a number: ")
This version calls a function named inputNumber() instead of input(). Unlike input(), which will accept any text, inputNumber() is smart and makes sure the user actually types a number. Cool, huh?
Well, it would be if the inputNumber() function actually existed. But, alas, it doesnât.
But, thereâs a solution. We can create the function ourselves. Hereâs the code for Func4.py:
Click here to view code image
# Numeric input function def inputNumber(prompt): # Input variable inp = "" # Loop until variable is a valid number while not inp.isnumeric(): # Prompt for input inp = input(prompt).strip() # Return the number return int(inp) # Get a number num=inputNumber("Enter a number: ") # Display it print(num)
Save and run the code. It will prompt you for a number, which will then be displayed. If you donât type a number, itâll keep asking you to Enter a number, and wonât stop until you actually enter a number.
The last two lines of code are pretty simple. inputNumber() works just like input(). It accepts a prompt and returns a value. Here the value is saved to a variable named num, which is then printed.
The real magic is the inputNumber() function itself. We start by defining it:
# Numeric input function def inputNumber(prompt):
Just like input(), inputNumber() accepts a promptâthe text that is displayed to the userâand the prompt is passed as an argument.
Next, the code defines a variable that will store the user input:
# Input variable inp = ""
Then comes the actual prompt inside of a loop:
Click here to view code image
# Loop until variable is a valid number while not inp.isnumeric(): # Prompt for input inp = input(prompt).strip()
This is code youâve seen before. It uses a while loop with a condition that ensures that the loop will keep looping until inp is a number.
The actual input() is the same one weâve been using throughout this book. What text does input() display as a prompt? Whatever gets passed to ÂinputNumber() as an argument. prompt is a passthrough variable: You pass it to our UDF ÂinputNumber(), which passes it to the built-in function input().
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26530)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19011)
The Fifty Shades Trilogy & Grey by E L James(18967)
Shot Through the Heart by Mercy Celeste(18885)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16992)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16882)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16810)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16706)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16334)
The Subtle Art of Not Giving a F*ck by Mark Manson(14269)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14073)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13435)
Scorched Earth by Nick Kyme(12718)
Drei Generationen auf dem Jakobsweg by Stein Pia(10926)
Suna by Ziefle Pia(10849)
Scythe by Neal Shusterman(10274)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9482)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9318)
This is Going to Hurt by Adam Kay(9109)
