Python: reverse guessing game

56 Views Asked by At

I wrote this code for a reverse guessing game but it prints c twice for the first time. Keep in mind (d=correct) (b=bigger) (k=smaller).

import random
c = random.randint(1,99)
print(c)
a = str(input())
while a != "d":
    if a == "b":
        print(c)
        c = random.randint(c,99)
        a = str(input())
    elif a == "k":        
        print(c)
        c = random.randint(1,c)
        a = str(input())
    elif a == "d":
     break

The output is like this:

67
b
67

I want 67 to be printed once.

I tried removing print(c) in lines 3, 7, 11.

2

There are 2 best solutions below

0
Saladin On BEST ANSWER

If you don't want to print c two times, simply try adding the print statement after generating the new random number as you did the first time.

0
Masih On

This should work

import random
c=random.randint(1,99)
print(c)
a=str(input())
while a!="d":
    if a=="b":
        c=random.randint(c,99)
        print(c)
        a=str(input())
    elif a=="k":
        c=random.randint(1,c)
        print(c)
        a=str(input())
    elif a=="d":
     break