vendredi 24 juillet 2020

Guessing game against the computer Python

I wrote this code for a guessing game against the computer using the random method. The programme work but when it turns to the user for the second round it can not continue. Can you help, please?

import random
global rand_val
rand_val=random.randint(10,50)

def start():
  ans=int(input("Enter a number between 10 and 50: "))
  if ans>=10 and ans<=50:
    new_rand_val=rand_val-ans
    if new_rand_val<=0:
      print("You guessed too far, try again\n")
      main()

    else:
      print("Amount left is,",new_rand_val," Now its the Computer turn\n")
    comput_turn(new_rand_val)

def comput_turn(new_rand_val):
  comp_guess=random.randint(0,new_rand_val) #the comupter is playing by guessing a number
  checking(comp_guess, new_rand_val)
  
def checking(comp_guess, new_rand_val):
    print("\n The computer chose:", comp_guess)#this is for me to follow up
    remain_rand_val=new_rand_val-comp_guess
    if remain_rand_val<=0:
      print("Computer guessed too far, now its trying again:")
      comput_turn(new_rand_val)


      userturn(remain_rand_val)
    print("Amount left is:", remain_rand_val," \n\n Now its your turn\n")
    userturn(remain_rand_val)

def userturn(remain_rand_val):
  ans=int(input("Enter a number between 0 and"))
  print(remain_rand_val)
  
  if ans>=10 and ans<=remain_rand_val:
    user_rand_val=remain_rand_val-ans

    print("Amount left is,",user_rand_val," Now its the Computer turn\n")
    comput_turn(user_rand_val)

def main():
  start()



Aucun commentaire:

Enregistrer un commentaire