-2

My code will always take the first input despite the input. I have tried taking guess and making guess into an int but that leads to both if statements to trigger

x = 1
answer = 4
A=0
guess = 0
while A < 10:
    for _ in range (5):
    x = random.randint(1,5)
    print ("Ralph holds up " + str(x) +  " fingers what is the answer")
    guess=input()
    if guess != answer:
        print("WRONG PROGRESS RESET")
        A=0
        answer = x
    if guess == answer:
        A += 1
        print ("correct")
        print ("You have " + str(A) + " out of 10 finished")
        answer = x
print ("You win congrats")
  • 5
    Please fix the indentation. It is important in python and for readability – Eugene Sh. Oct 18 '19 at 15:23
  • 2
    What is `answer` the first time through the loop? After setting `answer = x`, though, `guess != answer` will *always* be true, because `guess` is a `str` and `answer` is an `int`. – chepner Oct 18 '19 at 15:25
  • Several things to check: 1. What is `answer` the first time through the loop? 2. `x` is an integer, but `guess` is a string. – Andrew Jaffe Oct 18 '19 at 15:26
  • 2
    Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – Sayse Oct 18 '19 at 15:33
  • Ah, now I get is, this is like the guessing-game in Monkey Island II. Note, however, that (besides casting the input to `int`) you should use `elif` for the second `if`, otherwise the user could guess both wrong and right in the same turn (by just guessing `x`). – tobias_k Oct 18 '19 at 15:37

1 Answers1

0

First off your code never even import random to use randint. But I'll assume you did import it and you just never pasted it in.

input reads your input as a str, but you want it to read as an int. All you really need to do is wrap the input() call in an int invocation. Additionally, input takes an argument, which prompts that argument in the console.

Also @tobias_k has a good point, but instead of elif guess == answer:, just use else:.

I also changed some variable logic, and I changed A to a much more meaningful identifier, as well as fixing formatting, like a = b is more aesthetically pleasing than a=b.

Oh and indentation. Indentation is important, not only for readability, but in Python, it's required. Python is whitespace significant, meaning scope delimiters are whitespace.

import random
finished_count = 0

while finished_count < 10:
    for _ in range(5):
        answer = random.randint(1, 5)
        guess = int(input("Ralph holds up %d finger%s. What is the answer?\n" % (answer, "" if answer == 1 else "s")))
        if guess != answer: # incorrect answer
            print("WRONG PROGRESS RESET")
            finished_count = 0
        else: # correct
            finished_count += 1
            print("Correct. You have %d out of 10 finished" % finished_count)
print("You win, congrats!")

I just used this code up until I got to 10 and it works fine

incapaz
  • 349
  • 1
  • 3
  • 11