Saturday, February 17, 2018

User Inputs

Getting some information from the users:
The input() function pauses your program and waits for the user to enter some text.
name = input('Please enter your name?')
print("Hello " + name)

Also you store your prompt in a variable and pass that variable to the input():



question= "Good Morning!."
question+= "\nWhat is your first name? "
name = input(question)

print("\nHello, " + name + "!")

Output:
Good Morning!
What is your first name?
Hello xxx!

Also numeric values could be enter:
yourAge = input('How old are you?')
print(yourAge)  

Let's say we want to do this:
(1) print("You are " + yourAge + " old")
(2) print("In one year you will be: " + yourAge + 1)

This will cause an Error, because yourAge is considered an string, an strings cannot be added to numbers?
In order to do that we have to convert yourAge to int(cast). To do so we add int before yourAge:





yourAge = input('How old are you?')print("Your age:", int(yourAge) + 5)

Modulus Operator: 
number = input("Enter a number: ")
number = int(number)
if number % 2 == 0:
print("\nThe number " + str(number) + " is even.")
else:
print("\nThe number " + str(number) + " is odd.")


More on Input:

inputString = input( "> Say something\n")
print (inputString)
#basic info
firstname = input( "What is your name?\n")
lastname = input ("What 's lastname?\n")
address = input ( "What your address?\n")




cityState = input ("What's you city and state?\n")
birthday = input ("What 's your birthday\n")
email = input ("What is your email\n")
age = input("what is you age\n")
newAge = int (age)+1
myOldAge = int (age)-5
print( firstname + " "+lastname + "\n" + address + " " + cityState + "\n"+ birthday +"\n" + email)
print ("Your age next year will be " + str(newAge))
print ("Your age next year will be " + str(myOldAge))



No comments:

Post a Comment