So when I ran this code below, I noticed that my variables myName
and myAge
didn't function at all. An I getting something wrong here?
print ('Hello world!')
print ('What is your name?') # ask for their name
myName = input('Michael')
print ('It is good to meet you, ' + myName)
print('The length of your name is:')
print (len(myName))
print ('What is your age?') # ask for their age
myAge = input('16')
print('You will be " + str(int(myAge) + 1) + ' in a year.')
This line "print('You will be " + str(int(myAge) + 1) + ' in a year.')", you are using two different types of quotes ' and ", you can only use one type at once.
This is a string: 'Michael' Or like this: "Michael"
The items in quote above are valid strings in python.
"Michael' is an invalid string in python. Mixing two different types of quotation symbols.
Consider running my version of your code and see if it all makes sense.
print ('Hello world!')
myName = input('What is your name? ') # ask for their name
print ('It is good to meet you, ' + myName) # Shows the name
print('The length of your name is:')
print (len(myName)) # Shows the length of the name
myAge = input('What is your age? ') # ask for their age
print("You will be " + str(int(myAge) + 1) + " in a year.") # Shows the age + 1
If you type the input()
means that you expect an input from the user i guess you want something like
print ('Hello world!')
print ('What is your name?') # ask for their name
myName = 'Michael'
print ('It is good to meet you, ' + myName)
print('The length of your name is:')
print (myName)
print ('What is your age?') # ask for their age
myAge = '16'
print("You will be " + str(int(myAge) + 1) + " in a year.")
look that the variables are assigned as
variable = "value"
A Basic Logical mistake, while taking input following is Syntax Input Syntax
input("MessageYouWantToAsk")
Code Should be as follows
print ('Hello world!')
myName = input('What is your name?') # Ask for the name
print ('It is good to meet you, ' + myName) # prints the name and greets
print('The length of your name is:')
print (len(myName)) # Prints the length of that name
myAge = input('What is your Age?') # What is the input of the age
print('You will be ' + str(int(myAge) + 1) + ' in a year.') # prints the next year age
Keep in mind, whenever you open a ' string, you should disclose it with the same ' string closure.
Its a novice mistake to programming in python.
Thanks so much, guys...... It really helped learnt my mistake.... This is the correction:
print ('Hello world!')
print ('What is your name?') # ask for their name
myName = input('Michael')
myName = 'Michael'
print ('It is good to meet you, ' + myName)
print('The length of your name is:')
print (len(myName))
print ('What is your age?') # ask for their age
myAge = input('16')
myAge = 16
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.