Python Forum
4 tries choice question, one difficulty
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
4 tries choice question, one difficulty
#1
Hi there,

I have come up with this code, after 4 tries, it is supposed to say "Out of tries" but before it says "Out of tries", it will say "Try again"

Any idea what correction is needed to prevent "Try again" from appearing after the fourth try and have the code say "Out of tries" straight away?

rainbow = "red, orange, yellow, green, blue, indigo, violet"
tries = 0

rainbow = "red, orange, yellow, green, blue, indigo, violet"
tries = 0

while True:
    choice = input("Enter rainbow colour: ")
    if choice in rainbow:
        print("Correct")
        break
    else:
        tries += 1
        print("Try again")
        if tries == 4:
            print("Out of tries")
            break
        else:
            pass
Reply
#2
Hi,
You are counting tries += 1, and printing "try again", before you test for == 4.
There are several ways out of this :-)

Also, if you make one big string like "red, orange, yellow, green, blue, indigo, violet"
you don't need to put commas. Looks like it was an array before.
Paul
Reply
#3
Define rainbow as a list like
rainbow=['red','orange','yellow','green','blue','indigo','violet']
The reason I am telling you to do so is that in your code, python will even take it as a letter like this
Output:
Enter rainbow colour: o Correct
This happens as o is in the word orange and it will be printed because it is one big string, and python will take any letter from it and print it correct - this is your problem
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
pyzyx... shows how you need to use a list to fix one set of bugs.

Another is the logic of your loop. Write it out in English, then code each step.
1. Get the guess (you do this, though you may want to convert to lower case so you don't tell a guess of RED that it is wrong
2. If the guess is correct, inform the user and exit - you do this correctly also, though read on.
3. If the guess in incorrect, increment your counter. If the counter is 4 then print a losing message and exit. If the counter is less than 4 then print a message and loop back. This you do incorrectly. Do the steps in order.

Now, a minor peeve. I personally don't like while True: The reason is that you are setting up an infinite loop and trusting that you are catching every eventuality that requires a break. Minor difference, but I find the following to be easier to maintain
go = True 
while go:
    choice = input("Enter rainbow colour: ")
    if choice in rainbow:
        print("Correct")
        go = False
    else:
        tries += 1
        if tries == 4:
            print("Out of tries")
            go = False
        else:
            print("Try again")
Small differences from your code - define go, use go in the while statement, then change your breaks to making go False. And fixed the final logic. You were very close, still need to fix the code to make rainbow a list.
Reply
#5
Thanks for all the replies. Appreciate it. I've managed to solve it

rainbow = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")
tries = 0

go = True
while go:
    choice = input("Enter rainbow colour: ")
    if choice in rainbow:
        print("Correct")
        go = False
    else:
        tries += 1
        if tries == 4:
            print("Out of tries")
            go = False
        else:
            print("Try again")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Difficulty when trying to call all the input values extricate 2 1,957 Jun-05-2020, 09:36 AM
Last Post: pyzyx3qwerty
  Writing python function difficulty kirito85 5 3,302 Oct-28-2018, 07:34 AM
Last Post: buran
  Increasing difficulty of a maths game Maxxy_Gray 1 3,196 Apr-04-2018, 03:00 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020