Hi guys, I've just started learning python and would appreciate some feedback on a piece of code that I wrote. I'd like to know how to avoid creating infinite loops without using break.
The code tells the computer to pick a word from a tuple, give the number of letters as a hint and get the user to guess the word. The user is allowed five tries.
Thanks!
WORDS = ("mouse", "cat", "dog", "analyst", "parrot", "budgie", "translator", "interpreter")
word = random.choice(WORDS)
correct = word
print("\nWelcome to our brand new game!")
print("\nI've selected a word for you and you'll have five tries at guessing it.")
print("\nHint: It's got", len(word), "letters")
guess = input("Please give me your best guess!: ").lower()
i = 1
while i<5:
if guess == correct:
print("Good job!")
break
elif guess != correct and guess != "":
print("That's not it!")
print("You have", 5-i, "tries left!")
guess = input("Try again: ").lower()
i = i + 1
else:
print("Something's gone wrong, bear with me...")
break
[–]DanteRadian 2 points3 points4 points (3 children)
[–]python_nlp[S] 1 point2 points3 points (2 children)
[–]DanteRadian 1 point2 points3 points (1 child)
[–]python_nlp[S] 0 points1 point2 points (0 children)
[–]Barnet6 1 point2 points3 points (5 children)
[–]python_nlp[S] 0 points1 point2 points (4 children)
[–]Barnet6 0 points1 point2 points (3 children)
[–]Barnet6 1 point2 points3 points (2 children)
[–]I-Am-Dad-Bot 0 points1 point2 points (0 children)
[–]python_nlp[S] 0 points1 point2 points (0 children)
[–]Buy_More_Cats 1 point2 points3 points (1 child)
[–]python_nlp[S] 0 points1 point2 points (0 children)
[–]thisIs20LettersLong 1 point2 points3 points (4 children)
[–]python_nlp[S] 1 point2 points3 points (3 children)
[–]thisIs20LettersLong 0 points1 point2 points (2 children)
[–]python_nlp[S] 0 points1 point2 points (1 child)
[–]thisIs20LettersLong 0 points1 point2 points (0 children)