Answer the question
In order to leave comments, you need to log in
Guess the number game. How to make it so that after each entered number it writes 1 less attempt?
import random
import time
import sys
number = random.randint(1,10)
print("You have 4 tries!")
def ald():
for i in range(4):
time.sleep(0.5)
try:
a = int(input("Enter a number!: "))
except Exception:
print("This is not a number")
else:
if a > number:
print("The number is less than!")
elif a < number:
print("The number is greater")
elif a == number:
print("You win!")
break
else:
print("You lost")
ald()
g =input("Do you want it again?!: ")
if g ==("Yes"):
ald()
elif g ==("yes"):
ald()
elif g ==("no"):
print ("Bye")
sys.exit(0)
elif g ==("No"):
print("Bye")
sys.exit(0)
Answer the question
In order to leave comments, you need to log in
initially set the counter to 4. Something like this:
Then run a loop with checking attempts
attempts = 4
while attempts:
# сюда код с вводом числа и проверкой на правильность
attempts -= 1
Here is a small example
import random
import time
import sys
number = random.randint(1,3)
def game(num):
print("У тебя есть 4 попытки!")
while num>0:
num=num-1
user_num= int(input())
print('На 1 попытку меньше, у вас еще', num)
if user_num == number:
print('Вы угадали число!')
else:
restart()
def restart():
n= input('Хотите еще сыграть?')
if n=='Да':
game(4)
else:
break
game(4)
#game(4), 4 это число попытки
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question