Home > OS >  Python play again
Python play again

Time:02-12

I am trying to add a play again y/n option but the game asks me to pick a number before asking to play again (if it were to work) and the play again option doesn't pop up.

Code:

import sys
import os
import time
import random
import colorama
from colorama import Fore
import random
from sys import stdout as s
from time import sleep as j
def w(print):
  for i in print:
    j(.03)
    s.write(i)
    s.flush()
  next = input()
def noinput(print):
  for i in print:
    j(.03)
    s.write(i)
    s.flush()
while True:
  num = random.randint(1, 50)
  guess = "none"

  while guess != num:
    guess = input(Fore.CYAN   "Choose a number: ")
    guess = int(guess)

    if guess == num:
      w(Fore.GREEN   "You guessed the correct number!")
      break
    else:
      w(Fore.RED   "Sorry try again")

    if guess > num:
      w("your number is too large")
      os.system('clear')
    else:
      w("your number is too small")
      os.system('clear')

  play_again = input(Fore.BLUE   "Play again? y/n: ") 
  os.system('clear')
  if play_again.lower() != ("y"):
   break 

I would like to know why this is not working:

play_again = input(Fore.BLUE   "Play again? y/n: ") 
os.system('clear')
if play_again.lower() != ("y"):
 break 

Help appreciated

CodePudding user response:

Your code is not properly indented. Every control flow statement should be followed by an additional level of indentation.

As is we cannot comment on why your code doesn't work as expected, since it does not work at all :)

CodePudding user response:

Maybe you should specify the environment you run your code, such as python version, system version

however I just copied your code without change and it works fine on python3:

Choose a number: 49
You guessed the correct number!
Play again? y/n

PS: you use print as an argument of an function at line 10 def w(print):, but print is python's built-in function name, that will repoert an error python2, also wrong usage in python3 even no error is reported.

  • Related