How to create a guess number game in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to create a guess number game in Python?

I want to create a number guess game in Python. In which the programs creates a Random number and the user has to guess it.

31st Mar 2017, 4:12 PM
Zohaib Hassan
Zohaib Hassan - avatar
4 Answers
31st Mar 2017, 7:17 PM
Giorgi R.
Giorgi R. - avatar
+ 4
No I'm talking about IDE on PC
31st Mar 2017, 4:18 PM
Zohaib Hassan
Zohaib Hassan - avatar
+ 3
I believe you need something like this: from random import randint rand = randint(0, 10) for Trials in range(3): if int(input('Please guess the number:')) == rand: print(' Great, You Got It.') break else: print(' Wrong, Try Again.') if Trials < 2 else print(' Game Over.')
31st Mar 2017, 5:17 PM
Moataz El-Ibiary
Moataz El-Ibiary - avatar
+ 3
I found more efficient code than it: from time import sleep from random import randint from os import system while True: i = 0 num = randint(0,30) x = 0 while i <= 4: i += 1 guess = int(input("Enter you guess(0-30).")) if guess == num: print("You have guess well in {} attempts.".format(i)) x = 1 break elif guess <= num: print("Your guess is low.") print("Try again.") elif guess >= num: print("Your guess is high.") print("Try again.") if x == 0: print("You have ran out of 5 attempts.") print("Correct answer is {}".format(num)) choise = input("Do you want to try again.(y/n)") if choise == "y": pass else: exit()
31st Mar 2017, 6:21 PM
Zohaib Hassan
Zohaib Hassan - avatar