Answer to Question #205224 in Python for LEROY

Question #205224
# Looks great! # . I would come up with a better name for d

# test if the user wants to quit... 

# Is not quite right... Change this print('I thought of another number, try again')
#   to something more like "Not Quite, Please try again"



from random import randint




play = input('You want to start? y/n ')

def play_game():
    # Put the rules here
    # While loop goes here
    d = randint(7, 50)
    computer_num = randint(1, d)
    lives = d // 2
    while lives > 0:
        try:
            num = int(input(f'enter number from 1 to {d}, you have {lives} attempts left '))
            if num > d or num < 1:
                raise ValueError
        except:
            print('incorrect input, try again')
            continue
        if num == computer_num:
            print(f'you won, I thought number {computer_num}')
            return None
        else:
            lives -= 1
            if lives > 0:
                print('I thought of another number, try again')
    print(f'you lost, I thought number {computer_num}')

play = input('You want to start the game[y/n]: ')
while play.lower() == 'y':
    play_game()
    play = input('You want to play again? y/n ')
1
Expert's answer
2021-06-10T05:16:35-0400
from random import randint

play = input('You want to start? y/n ')

def play_game():
  # Put the rules here
  # While loop goes here
  game_size = randint(7, 50)
  computer_num = randint(1, game_size)
  lives = game_size // 2
  while lives > 0:
    try:
      num = int(input(f'Enter number from 1 to {game_size}, you have {lives} attempts left: '))
      if num > game_size or num < 1:
        raise ValueError
    except:
      print('Incorrect input, try again.')
      continue
    if num == computer_num:
      print(f'You won, I guessed this number! {computer_num}')
      return None
    else:
      lives -= 1
      if lives > 0:
        print('Good try, you still have a chance!')
  print(f'You lost, I guessed this number! {computer_num}')

play = input('You want to start the game[y/n]: ')
while play.lower() == 'y':
  play_game()
  play = input('You want to play again? y/n ')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS