我正在为学校做作业,这个程序很容易,但我的if语句不能正常工作。有人知道为什么吗?这是代码:
letter = input("Name one of the most common letters in wheel of fortune puzzles: ")
if letter == "r":
print(letter, "is one of the most common letters!")
if letter == "s":
print(letter, "is one of the most common letters!")
if letter == "t":
print(letter, "is one of the most common letters!")
if letter == "l":
print(letter, "is one of the most common letters!")
if letter == "n":
print(letter, "is one of the most common letters!")
if letter == "e":
print(letter, "is one of the most common letters!")
else:
print("Incorrect!")
letter = input("Name one of the most common letters in wheel of fortune puzzles: ")
input("\n\nPress the enter key to exit:")
输出是:
Name one of the most common letters in wheel of fortune puzzles: j
Incorrect!
Name one of the most common letters in wheel of fortune puzzles: r
Press the enter key to exit:
答案 0 :(得分:5)
我会写这样的东西:
# Assumes Python 3
# Python 2: Use `raw_input` instead of input
common_letters = 'rstlne'
prompt = "Name one of the most common letters in wheel of fortune puzzles: "
while True:
letter = input(prompt).strip()
if letter in common_letters:
print(letter, "is one of the most common letters!")
break
else:
print("Incorrect!")
answer = input('Type "end" to exit: ')
if answer.strip() == 'end':
break
我改变了一些事情:
我使用了if
而不是那么多if letter in common_letters:
个语句。这样您就可以添加矿石,从common_letters
删除另一个字母。
我使用input(prompt).strip()
来删除额外的空白区域。
如果没有输入常用字母,我会使用while True
循环一遍又一遍地重复这个问题。 break
终止此循环,即程序已完成。
答案 1 :(得分:0)
您需要输入"r"
或简单使用raw_input()
。raw_input接受string
,input
= eval(raw_input(prompt)).
不是这样。所以它会搜索r
变量,除非你说"r"
告诉它它是一个字符串。
答案 2 :(得分:0)
您可以在代码中进行以下更改。
从第二个if
条件更改为elif
,因为如果您输入例如's',那么它将进入第二if
条件以及{{1部分将运行。
将else
更改为input
示例更改
raw_input
到
letter = input("Name one of the most common letters in wheel of fortune puzzles: ")
答案 3 :(得分:0)
#considering you enter 'j' and 'r' here's the path your code takes
letter = input("Name one of the most common letters in wheel of fortune puzzles: ")
# letter now = 'j'
if letter == "r": # letter don't equal 'r', don't do next line
print(letter, "is one of the most common letters!")
if letter == "s": # letter don't equal 's', don't do next line
print(letter, "is one of the most common letters!")
if letter == "t": # letter don't equal 't', don't do next line
print(letter, "is one of the most common letters!")
if letter == "l": # letter don't equal 'l', don't do next line
print(letter, "is one of the most common letters!")
if letter == "n": # letter don't equal 'n', don't do next line
print(letter, "is one of the most common letters!")
if letter == "e": # letter don't equal 'e', don't do next line
print(letter, "is one of the most common letters!")
else: # because last if was false (letter don't equal 'e') do next line.
print("Incorrect!") # output : Incorrect!
# next line is not consider inside else. should be included within {} if desired
letter = input("Name one of the most common letters in wheel of fortune puzzles: ")
# letter now equals 'r'
#nothing to do with it
input("\n\nPress the enter key to exit:")
# showing Press the enter key to exit: and waiting.
这会像我想的那样奏效。
letter = raw_input("Name one of the most common letters in wheel of fortune puzzles: ")
if letter == "r":
print(letter, "is one of the most common letters!")
elif letter == "s":
print(letter, "is one of the most common letters!")
elif letter == "t":
print(letter, "is one of the most common letters!")
elif letter == "l":
print(letter, "is one of the most common letters!")
elif letter == "n":
print(letter, "is one of the most common letters!")
elif letter == "e":
print(letter, "is one of the most common letters!")
else:
print("Incorrect!")
raw_input("\n\nPress the enter key to exit:")