Hot / Cold Game in Python

I am making my way through the Python Codex Academy course and trying to build small side projects to help strengthen the lessons.

I am currently working on several games. I want the program to select a random number from 1 to 10 and the user to enter a guess.

Then the program will return a message that you won, or an invitation to choose another higher / lower number.

My code is below. I can not get him to repeat the process with the second user input.

I do not need an answer, just a hint.

import random
random.seed()

print "Play the Number Game!"
x = raw_input("Enter a whole number between 1 and 10:")
y = random.randrange(1, 10, 1)

#Add for loop in here to make the game repeat until correct guess?
if x == y:
    print "You win."
    print "Your number was ", x, " and my number was ", y
elif x > y:
    x = raw_input("Your number was too high, pick a lower one: ")
elif x < y:
    x = raw_input("Your number was too low, pick a higher one: ")
+4
source share
1 answer

You need to use a loop whilelike while x != y:. Here is more information about the while loop.


import random
y = random.randint(1, 10)

random.


, int() .

:)

import random
n = random.randint(1, 10)
g = int(raw_input("Enter a whole number between 1 and 10: "))

while g != n:

    if g > n:
        g = int(raw_input("Your number was too high, pick a lower one: "))

    elif g < n:
        g = int(raw_input("Your number was too low, pick a higher one: "))

else:
    print "You win."
    print "Your number was ", g, " and my number was ", n
+4

Source: https://habr.com/ru/post/1608308/


All Articles