-2

i want to create a random number generator with a try counter but it said "cant assign to operator" here is what i have so far

  import random
    number=random.randint(1,10)
    print ("i am thinking of a number between 1 and 10")
    counter=5
    while counter>0:
        if number==int(input("guess what the number is:  ")):
            print("well done")
        else:
            counter-1=counter #it displays it hear before the 1st counter
            print ("your bad try again")
    print ("it was" ,number,)
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
joe
  • 1

3 Answers3

0

It's probably because of

counter-1=counter 

But you should post your code clearly formatted so it's easy to see where the error is!

Quan Vuong
  • 1,919
  • 3
  • 14
  • 24
0

You can not modify a variable name:

 import random
    number = random.randint(1, 10)
    print("i am thinking of a number between 1 and 10")
    counter = 5
    while counter > 0:
        if number == int(input("guess what the number is:  ")):
            print("well done")
        else:
            counter = counter - 1 # you cant modify a variable name
            print("your bad try again")
    print("it was", number, )

Output:

i am thinking of a number between 1 and 10
guess what the number is:  5
your bad try again
guess what the number is:  4
your bad try again
guess what the number is:  3
your bad try again
guess what the number is:  2
your bad try again
guess what the number is:  1
your bad try again
it was 7
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
0

The problem with you code was on how you decrement the counter variable.

Here is a better solution with for loop. It does the decrementing for you.

import random
number=random.randint(1,10)
print ("I am thinking of a number between 1 and 10")
print ("Guess what the number is - ",end='')
for __ in range(5):
    if number==int(input()):
        print("well done")
        break
    else:
        print ("your bad try again")
print ("Number was " ,number)
Gurupad Mamadapur
  • 989
  • 1
  • 13
  • 24