程序错误地要求用户输入两次

时间:2019-01-17 19:20:42

标签: python raw-input

编写依次执行以下操作的程序: 要求用户输入他/她的生日。 要求用户输入他/她的姓氏。 按此顺序打印用户的姓氏和出生日期。

我正在上一门计算机科学课,这是入门级的问题集。我得到了正确的答案,但我不太明白为什么它要求“用户”输入两次姓名和生日。

我尝试编辑代码的前两行,即“原始打印”,我尝试在x和y =命令上方添加#descriptions,并且还编辑了几次打印您的姓名和生日的行。不太确定我缺少什么。

print raw_input('What is your birthday?') 
print raw_input('What is your name?')

x = raw_input('What is your birthday?')

y = raw_input('What is your name?')

print "Your name and birthday are " + y + x

预期:

What is your birthday?1122
1122
What is your name?rudy
rudy
Your name and birthday are Rudy 1122

实际结果:

What is your birthday?1122
1122
What is your name?rudy
rudy
What is your birthday?1122
What is your name?rudy
Your name and birthday are rudy1122
>>> 

2 个答案:

答案 0 :(得分:1)

获得两次提示的原因是,每次调用raw_input('x')时,它将在控制台上提示用户。您有两个print语句在脚本顶部执行此操作,然后在后面的两个调用中捕获了用户对变量的输入。

raw_input会自动将括号中的短语打印给用户,因此您不需要先在脚本顶部打印它们,可以删除这些行,您应该会得到预期的结果。 / p>

答案 1 :(得分:0)

您有两行可以打印

print raw_input('What is your birthday?') # this prints but you don't get the input from it
print raw_input('What is your name?') #this also prints

x = raw_input('What is your birthday?') #this and the following line are all you need

y = raw_input('What is your name?')