Skip to main content
Wow sorry, that code was totally wrong! Fixed up the code sample.
Source Link
Spencer
  • 261
  • 1
  • 11

You could add a "while" loop instead of an "if" statement. This will allow the user to keep trying until it is correct, and is very pythonic.

Also if you want to be really clean review PEP8 "function and variable names". You'll want your variables lower case, with underscores "_". Python Zen says "Explicit is better than implicit." Therefore I would change around your variable names to be more readable.

For example:

import random

max_num = 10
random_num = random.randint(0, max_num) 

print("Guess a number:")
guess = int(input()) 

while not guess == random_num:
    print("Sorry, wrongtry number.again:")
    guess = int(input())
print("You got it!")

I would also suggest adding a try-except block around the input() call to prevent the code from crashing if the user inputs anything other than an integer. In Python int("a string") will return a ValueError. So you would want to provide for this potential exception - alert the user accordingly and allow them to try again.

You could add a "while" loop instead of an "if" statement. This will allow the user to keep trying until it is correct, and is very pythonic.

Also if you want to be really clean review PEP8 "function and variable names". You'll want your variables lower case, with underscores "_". Python Zen says "Explicit is better than implicit." Therefore I would change around your variable names to be more readable.

For example:

import random

max_num = 10
random_num = random(0, max_num)

guess = input()
while not guess == random_num:
    print("Sorry, wrong number.")
    guess = input()
print("You got it!")

You could add a "while" loop instead of an "if" statement. This will allow the user to keep trying until it is correct, and is very pythonic.

Also if you want to be really clean review PEP8 "function and variable names". You'll want your variables lower case, with underscores "_". Python Zen says "Explicit is better than implicit." Therefore I would change around your variable names to be more readable.

For example:

import random

max_num = 10
random_num = random.randint(0, max_num) 

print("Guess a number:")
guess = int(input()) 

while not guess == random_num:
    print("Sorry, try again:")
    guess = int(input())
print("You got it!")

I would also suggest adding a try-except block around the input() call to prevent the code from crashing if the user inputs anything other than an integer. In Python int("a string") will return a ValueError. So you would want to provide for this potential exception - alert the user accordingly and allow them to try again.

further expansion
Source Link
Spencer
  • 261
  • 1
  • 11

You could add a while"while" loop instead of an if"if" statement. This will allow the user to keep trying until it is correct, and is very pythonic.

Also if you want to be really clean review PEP8 "function and variable names". You'll want your variables lower case, with underscores "_". Python Zen says "Explicit is better than implicit." Therefore I would change around your variable names to be more readable.

For example:

import random

max_num = 10
random_num = random(0, max_num)

guess = input()
while not guess == rnrandom_num:
    print("Sorry, wrong number.")
    guess = input()
print("You got it!")

You could add a while loop instead of an if statement:

guess = input()
while not guess == rn:
    print("Sorry, wrong number.")
    guess = input()
print("You got it!")

You could add a "while" loop instead of an "if" statement. This will allow the user to keep trying until it is correct, and is very pythonic.

Also if you want to be really clean review PEP8 "function and variable names". You'll want your variables lower case, with underscores "_". Python Zen says "Explicit is better than implicit." Therefore I would change around your variable names to be more readable.

For example:

import random

max_num = 10
random_num = random(0, max_num)

guess = input()
while not guess == random_num:
    print("Sorry, wrong number.")
    guess = input()
print("You got it!")
Source Link
Spencer
  • 261
  • 1
  • 11

You could add a while loop instead of an if statement:

guess = input()
while not guess == rn:
    print("Sorry, wrong number.")
    guess = input()
print("You got it!")