-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_the_number.py
38 lines (30 loc) · 1.3 KB
/
guess_the_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# This project wants the user to write a number and the programm (with the help of a library) chooses a random number between 0-100.
import random
ran_num = random.randint(0, 100)
# now the computer chooses numbers between 0-100
counter = 0
# counts the attempts
while True:
try:
number = int(input("Enter an integer number: "))
except ValueError:
print("Invalid input! ")
# better try again... Return to the start of the loop
continue
if number > 100 or number < 0:
print("Choose your number between 0 - 100 ")
# try again... Return to the start of the loop
elif number < ran_num:
print("The number you guessed is too low, Try again!")
counter+=1
elif number > ran_num:
print("The number you guessed is too high, Try again!")
counter+=1
else:
print("Congratulations!You guessed the number right!\nNumber of attempts: "+str(counter))
break
# Sources
# https://wiki.python.org/moin/HandlingExceptions
# https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response
# https://pynative.com/python-random-randrange/
# https://stackoverflow.com/questions/18348717/how-to-use-concatenate-a-fixed-string-and-a-variable-in-python