I have a question for Python

Asked 2 years ago, Updated 2 years ago, 14 views

I'm going to give you two chances to guess the answer, and I'm going to give you the first comparison in the second chance.

If I run as below, I can't get the second input value and it comes out as a string, so if I add the input function, I get an error, so can you tell me how?

from random import randint

answer=randint(1,9)
print ("Welcome to the numbers game.")
g=input ("Guess the number: ")
guess=int(g)

if (guess==answer):
    print("User won.")
elif (answer>guess):
    Print(guess), try again with a larger number: ")
    if(answer==guess) :
      print("User fitted.")
    else:
      print ("You Are a Fool")
      print ("End of Game")
else:
    Print(guess), try again with a smaller number: ")
    if answer==guess:
      print("User fitted.")
    else:
      print ("You are a fool".")
      print ("End of Game")

python

2022-09-21 10:16

1 Answers

You can do it like below.

I think it's because you didn't change the type to int.

from random import randint

answer=randint(1,9)

print(answer)

print ("Welcome to the numbers game.")
g=input ("Guess the number: ")
guess=int(g)

if (guess==answer):
    print("User won.")

elif (answer>guess):
    Guess = int (re-align to a number greater than "")
    print(guess, ",", answer)

    if(answer==guess) :
      print("User fitted.")

    else:
      print ("You Are a Fool")
      print ("End of Game")

else:
    Guess = int (input (re-align to a number less than "")

    if answer==guess:
      print("User fitted.")

    else:
      print ("You are a fool".")
      print ("End of Game")

However, in the case of these codes, it is recommended to prevent reuse of the code as much as possible and intuitively do it as below.

from random import randint

answer=randint(1,9)

print ("Welcome to the numbers game.")
Guess the number = int (input ("guess the number: ")))


if (guess==answer):
    print("User won.")

else:
    if(answer>guess):
        print (re-align to a number greater than ")
    elif(answer<guess):
        print (re-align to a number less than ")

    guess = int(input())

    if(answer==guess) :
      print("User fitted.")

    else:
      "You are a fool 
End of game""")


2022-09-21 10:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.