K
K
kurle2021-09-19 13:04:14
Python
kurle, 2021-09-19 13:04:14

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

2 answer(s)
L
low molecular macro, 2021-09-19
@kurle

initially set the counter to 4. Something like this: Then run a loop with checking attempts
attempts = 4

while attempts:
   # сюда код с вводом числа и проверкой на правильность
   attempts -= 1

I
Insaf Fretch, 2021-09-19
@ishaimiev

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 question

Ask a Question

731 491 924 answers to any question