If the instruction in python does not print

I am working on a school assignment, and this SUPER program is simple, but the if statement is not working properly. Does anyone know why? Here is the code:

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

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
source share
4 answers

I would write something like this:

# 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

There are a few things that I changed:

  • Instead of many operators ifI used if letter in common_letters:. This allows you to simply add ore to remove another letter to / from common_letters.

  • I use input(prompt).strip()to highlight the extra space.

  • I use a loop while Trueto repeat the question over and over again if no common letter has been entered. breakcompletes this cycle, i.e. The program ends.

+5

"r" raw_input().raw_input string, input= eval(raw_input(prompt)). r, "r", , .

0

In your code you can make the following changes.

  • From the second condition, ifchange it to elif, because if you enter, for example, 's', then it will go into the second condition if, and it will also be.

  • change inputto raw_inputchange example

letter = input("Name one of the most common letters in wheel of fortune puzzles: ") to

letter = raw_input("Name one of the most common letters in wheel of fortune puzzles: ")

0
source
#considering you enter 'j' and 'r' here 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.

This will work as I think you want.

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:")
0
source

Source: https://habr.com/ru/post/1618996/


All Articles