简体   繁体   English

为什么我在第10行收到无效的语法?

[英]Why am i getting Invalid syntax at line 10?

 # this program says hello and ask my name.
 print('Hello World')
 print('Whats your name?')
 myname = input()
 print('its good to meet you, ' + myname) #ask for my name
 print('the length of your name is : ')
 print(len(myname))
 print('whats your age?')#ask for my age
 myage = input()
 print('you  will be ' + str(int(myage) + 1) + 1 'in a year')

I see syntax error at line 10 of the code i see all the parenthesis are correctly placed. 我在代码的第10行看到语法错误,我看到所有括号均已正确放置。

I think this is what you want based on the logic of your program 我认为这是基于程序逻辑的所需

print('you will be ' + str(int(myage) + 1) + ' in a year')

output: 输出:

Hello World
Whats your name?
Jon Doe
its good to meet you, Jon Doe
the length of your name is : 
7
whats your age?
23
you will be 24 in a year

Edit: OP wants me to explain when to use + in print() . 编辑:OP要我解释一下何时在print()使用+ usually if you are printing more than one arguments in your print() statement you want to join them using + . 通常,如果要在print()语句中打印多个参数,则希望使用+来加入它们。 and when you join arguments together remember their type have to be the same. 当您将参数连接在一起时,请记住它们的type必须相同。 so for example. 例如。 print(1 + '2') would give you an error because 1 is a int and '2' is a str . print(1 + '2')会给你一个错误,因为1int'2'str you would have to cast the arguments to make them the same. 您必须转换参数以使其相同。 so print(1 + int('2')) would correctly give you 3 所以print(1 + int('2'))会正确地给你3

You got an extra 1 . 您多了1 change print('you will be ' + str(int(myage) + 1) + 1 'in a year') to 更改print('you will be ' + str(int(myage) + 1) + 1 'in a year')

print('you will be ' + str(int(myage) + 1) + ' in a year')

Check your code here 在这里检查您的代码

Try : 尝试:

# this program says hello and ask my name.
print('Hello World')
print('Whats your name?')
myname = input()
print('its good to meet you, ' + myname) #ask for my name
print('the length of your name is : ')
print(len(myname))
print('whats your age?')#ask for my age
myage = input()
print('you  will be ' + str(int(myage) + 1) + ' in a year')

Following will solve your problem: 以下将解决您的问题:

print('Hello World')
print('Whats your name?')
myname = raw_input()
print('its good to meet you, ' + myname) #ask for my name
print('the length of your name is : ')
print(len(myname))
print('whats your age?')#ask for my age
myage = raw_input()
print("you  will be " + str(int(myage) + 1) + " years old by next year")

You have missed out the '+' in your line 10 to concatenate your strings. 您错过了第10行中的“ +”来连接字符串。 Also there was an unwanted '+ 1' which would have caused another error for you as you were trying to concatenate int and str objects. 此外,在尝试连接int和str对象时,还会出现一个不必要的“ +1”,这会给您带来另一个错误。

Another correction, I have changed the input() function with raw_input() , as I am using python2. 另一个更正,因为我使用的是python2,所以我用raw_input()更改了input()函数。 If you need any better explanation on this chnage, you could check this link . 如果您需要有关此更改的更好说明,可以查看此链接

you should have a string in this command: 您应该在此命令中包含一个字符串:

print('you  will be ' + str(int(myage) + 1) + 1 'in a year')

these are your strings: 'you will be ' and str(int(myage) + 1) and 'in a year' . 这些是您的字符串: 'you will be'str(int(myage)+ 1)'in a year' but you are wrong in writing 1 'in a year' . 但是您写“一年”中的错误。 this is not string. 这不是字符串。 i think you don't need to the second "1". 我认为您不需要第二个“ 1”。 you can write: 你可以写:

print('you  will be ' + str(int(myage) + 1) +'in a year')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM