Skip to main content
edited tags
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
added 3 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Best Practices: What can I do How to make this random number game better?

ImI'm a newbie trying to teach myself pythonPython. Could somebody tell me if there is a better way to write this. I? I am also curious as to ifwhether or not there is anything I should not do.This This code does work...Some, but some indentation may have been lost when I pasted it..

Thanks!

import random
 

def game():
    yes = ['y', 'Y', 'Yes', 'yes']
    number = random.randint(1, 20)
    numAttempts = 0
    print('')
    name = input('What is your name? ')

    print('')
    print('Nice to meet you', name, '! I am going to guess a random number! You will have 3 tries to guess it. '
                                'Good luck!')


    while numAttempts < 3:
        print('')
        guess = int(input('Guess the number I have chosen between 1 and 20. '))
        numAttempts += 1
        if guess == number:
            print('')
            print('You guessed correctly! The number was', number, "!", 'Great Job', name, '!')
            break
        elif guess > number:
            print('')
            print('The number is lower than', guess, "!")
        elif guess < number:
            print('')
            print('The number is Higher than', guess, "!")
    else:
        print('')
        print('Sorry, the number I chose was', number, 'but you have used all 3 tries', name, '!')

    print('')
    gameover = input("Would you like to play again? Yes or No? ")
    while gameover in yes:
        game()
    else:
        print('')
        print('You should have chose yes. You computer will now explode!') 

game()

game()

Best Practices: What can I do better?

Im a newbie trying to teach myself python. Could somebody tell me if there is a better way to write this. I am also curious as to if there is anything I should not do.This code does work...Some indentation may have been lost when I pasted it..

Thanks!

import random
 

def game():
    yes = ['y', 'Y', 'Yes', 'yes']
    number = random.randint(1, 20)
    numAttempts = 0
    print('')
    name = input('What is your name? ')

    print('')
    print('Nice to meet you', name, '! I am going to guess a random number! You will have 3 tries to guess it. '
                                'Good luck!')


    while numAttempts < 3:
        print('')
        guess = int(input('Guess the number I have chosen between 1 and 20. '))
        numAttempts += 1
        if guess == number:
            print('')
            print('You guessed correctly! The number was', number, "!", 'Great Job', name, '!')
            break
        elif guess > number:
            print('')
            print('The number is lower than', guess, "!")
        elif guess < number:
            print('')
            print('The number is Higher than', guess, "!")
    else:
        print('')
        print('Sorry, the number I chose was', number, 'but you have used all 3 tries', name, '!')

    print('')
    gameover = input("Would you like to play again? Yes or No? ")
    while gameover in yes:
        game()
    else:
        print('')
        print('You should have chose yes. You computer will now explode!')

game()

How to make this random number game better?

I'm a newbie trying to teach myself Python. Could somebody tell me if there is a better way to write this? I am also curious as to whether or not there is anything I should not do. This code does work, but some indentation may have been lost when I pasted it.

import random

def game():
    yes = ['y', 'Y', 'Yes', 'yes']
    number = random.randint(1, 20)
    numAttempts = 0
    print('')
    name = input('What is your name? ')

    print('')
    print('Nice to meet you', name, '! I am going to guess a random number! You will have 3 tries to guess it. '
                                'Good luck!')


    while numAttempts < 3:
        print('')
        guess = int(input('Guess the number I have chosen between 1 and 20. '))
        numAttempts += 1
        if guess == number:
            print('')
            print('You guessed correctly! The number was', number, "!", 'Great Job', name, '!')
            break
        elif guess > number:
            print('')
            print('The number is lower than', guess, "!")
        elif guess < number:
            print('')
            print('The number is Higher than', guess, "!")
    else:
        print('')
        print('Sorry, the number I chose was', number, 'but you have used all 3 tries', name, '!')

    print('')
    gameover = input("Would you like to play again? Yes or No? ")
    while gameover in yes:
        game()
    else:
        print('')
        print('You should have chose yes. You computer will now explode!') 

game()
Source Link

Best Practices: What can I do better?

Im a newbie trying to teach myself python. Could somebody tell me if there is a better way to write this. I am also curious as to if there is anything I should not do.This code does work...Some indentation may have been lost when I pasted it..

Thanks!

import random


def game():
    yes = ['y', 'Y', 'Yes', 'yes']
    number = random.randint(1, 20)
    numAttempts = 0
    print('')
    name = input('What is your name? ')

    print('')
    print('Nice to meet you', name, '! I am going to guess a random number! You will have 3 tries to guess it. '
                                'Good luck!')


    while numAttempts < 3:
        print('')
        guess = int(input('Guess the number I have chosen between 1 and 20. '))
        numAttempts += 1
        if guess == number:
            print('')
            print('You guessed correctly! The number was', number, "!", 'Great Job', name, '!')
            break
        elif guess > number:
            print('')
            print('The number is lower than', guess, "!")
        elif guess < number:
            print('')
            print('The number is Higher than', guess, "!")
    else:
        print('')
        print('Sorry, the number I chose was', number, 'but you have used all 3 tries', name, '!')

    print('')
    gameover = input("Would you like to play again? Yes or No? ")
    while gameover in yes:
        game()
    else:
        print('')
        print('You should have chose yes. You computer will now explode!')

game()