@daria20022001

Как сделать бесконечный цикл но только при ответе «yes»?

while True:
 import random
 print("-------Guess my number-------")
 print("You need to guess the number picked by enigmatic computer!")
 print("Number is in range from 1 to 10")
 magic_number = random.randint(1, 10)
 count = 0
 user_number = 0
 while user_number != magic_number:
  user_number = int(input("Your number: "))
  count+=1
  if magic_number > user_number:
   print("The magic number is greater than yours!")
  elif magic_number < user_number:
   print("The magic number is less than yours!")
 print("You Win! You guessed it on the", count,
 "try")
 while True:
   print ("Want to play again?")
   answer = str(input())
   if answer == ("no"):
     print ("Okay, see you later")
     break
   if answer == ("yes"):
     import random
     print("-------Guess my number-------")
     print("You need to guess the number picked by enigmatic computer!")
     print("Number is in range from 1 to 10")
     magic_number = random.randint(1, 10)
     count = 0
     user_number = 0
     while user_number != magic_number:
      user_number = int(input("Your number: "))
      count+=1
      if magic_number > user_number:
       print("The magic number is greater than yours!")
      elif magic_number < user_number:
       print("The magic number is less than yours!")
   print("You Win! You guessed it on the", count,
   "try")

нужно чтобы цикл продолжался только при ответе "yes"
  • Вопрос задан
  • 82 просмотра
Решения вопроса 1
@nvlveu
Не нужно создавать цикл в цикле)

Как-то так:
import random


count = 0


print("-------Guess my number-------")
print("You need to guess the number picked by enigmatic computer!")
print("Number is in range from 1 to 10")

user_number = None


magic_number = random.randint(1, 10)


while user_number != magic_number:
    magic_number = random.randint(1, 10)

    user_number = int( input("Your number: ") )
    count += 1

    if magic_number > user_number:
        print("The magic number is greater than yours!")
    elif magic_number < user_number:
        print("The magic number is less than yours!")
        print("You Win! You guessed it on the", count, "try")

    print("Want to play again?")
    answer = input()
    if answer == "no":
        print("Okay, see you later")
        break
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы