Answer to Question #204318 in Python for LEROY

Question #204318

Make a games (or any other kind of program that you find interesting). Make sure you pick something that you are probably capable of doing. The point of this project is to show off your skills, and to create something very playable, with clear instructions and a great user experience. It’s much better to get a simple program working perfectly than to try to make an overly complicated game that ends up being very glitchy. Also, you must be able to competently explain every bit of your code.


1
Expert's answer
2021-06-07T17:20:45-0400
# -*- coding: utf-8 -*-
from random import randint


def play_game():
    
    d = randint(7, 16)
    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
        else:
            lives -= 1
            if lives > 0:
                print('I thought of another number, try again')
    print(f'you lost, I thought number {computer_num}')
    
        


rules = '''
when you start the game,
the computer will guess a random number in a random range.
to win you need to guess this number in a limited number of attempts
'''
print(rules)


play = input('You want to start? 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