counter=0
initials=0
name1=raw_input("Please enter your first name!")
name2=raw_input("Please enter your middle name!")
name3=raw_input("Please enter your last name!")
option=str(raw_input("a)Print the length of my name\nb)Print\nc)Exit\nChoose one of the options."))
while option != "c" or option != "C":
if (option=="a" or option=="A"):
print "Your first name has " + str(len(name1)) + " letters."
print "Your second name has " + str(len(name2)) + " letters."
print "Your last name has " + str(len(name3)) + " letters."
elif (option=="b" or option=="B"):
print name1[0] + "." + name2[0] + "." + name3[0]
elif (option=="c" or option=="C"):
break
Run Code Online (Sandbox Code Playgroud)
这是我的代码.由于某种原因,它一直进入无限循环.一旦用户选择他们的选项,我该如何停止?我只需要输入用户选择一次的选项.
您需要添加该行
option=str(raw_input("a)Print the length of my name\nb)Print\nc)Exit\nChoose one of the options."))
Run Code Online (Sandbox Code Playgroud)
在while循环结束时.这将再次要求用户输入,如果是c或C,它会退出.
你也应该改变你的while状况.使用or将导致它始终False.您可能应该使用while True(因为c在循环内部检查输入).
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |