解析时自动执行无聊的东西,意外的EOF

时间:2019-06-26 00:04:25

标签: python input

我复制了Al Sweitgart在他的第3课视频“自动化无聊的东西”中给出的代码。运行模块

时出现错误
  

解析时出现意外的EOF

在最后一行,我试图写

print('You will be " + str(int(MyAge + 1) + " in a year.')

代码将运行,但不会添加使用期限,并显示字符串:

You will be " + str(int(MyAge + 1) + " in a year.

该程序打招呼并询问我的名字:

print ('Hello World!')
print ('What is your name?') # ask for their name
myName = input()
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()
print('You will be ' + str(int(MyAge + 1) + ' in a year.')

如果用户输入年龄的29,则输出应为

You will be 30 in a year.

而不是

You will be " + str(int(MyAge + 1) + " in a year.

1 个答案:

答案 0 :(得分:0)

使用以下代码。请注意,在倒数第二行使用int并在最后一行删除int。

# This program says hello and asks for my  name

print ('Hello World!')

print ('What is your name?') # ask for their name
myName = input()
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 = int(input()) # CASTING TO AN INT ON THIS LINE 
print('You will be ' + str(myAge + 1) + ' in a year.') # CHANGING TO A STRING ON THIS 
# LINE AND REMOVING THE INT FUNCTION