如果python中的语句没有打印

时间:2015-12-08 04:53:50

标签: python python-3.x if-statement

我正在为学校做作业,这个程序很容易,但我的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:

4 个答案:

答案 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

我改变了一些事情:

  1. 我使用了if而不是那么多if letter in common_letters:个语句。这样您就可以添加矿石,从common_letters删除另一个字母。

  2. 我使用input(prompt).strip()来删除额外的空白区域。

  3. 如果没有输入常用字母,我会使用while True循环一遍又一遍地重复这个问题。 break终止此循环,即程序已完成。

答案 1 :(得分:0)

您需要输入"r"或简单使用raw_input()。raw_input接受stringinput = eval(raw_input(prompt)).不是这样。所以它会搜索r变量,除非你说"r"告诉它它是一个字符串。

答案 2 :(得分:0)

您可以在代码中进行以下更改。

  1. 从第二个if条件更改为elif,因为如果您输入例如's',那么它将进入第二if条件以及{{1部分将运行。

  2. else更改为input示例更改

  3. 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:")