How can i improve this? [closed]
I am currently self teaching myself Python , and this is my first program. I am doing some simple projects so i can improve. Would like to hear feedback on this one.
import random
print("There is a number between 1 and 100 guess it and you will win a price : ")
def guess():
# A simple guess game.
random_number = random.randint(1, 5)
flag = True
user_guess =
while flag:
user_number = input("Enter the number : ")
user_guess.append(user_number)
if int(user_number) < random_number:
print("You have to go higher")
elif int(user_number) > random_number:
print("You have to go lower ")
else:
flag = False
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(len(user_guess))
guess()
while True:
play_again = input("Do you like to play again ? : (y/n): ")
if play_again == 'y':
guess()
else:
print("Thank you for playing have a nice day!!! ")
break
python
closed as too broad by Chris_Rands, chepner, usr2564301, msanford, JJJ Nov 14 '18 at 17:56
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am currently self teaching myself Python , and this is my first program. I am doing some simple projects so i can improve. Would like to hear feedback on this one.
import random
print("There is a number between 1 and 100 guess it and you will win a price : ")
def guess():
# A simple guess game.
random_number = random.randint(1, 5)
flag = True
user_guess =
while flag:
user_number = input("Enter the number : ")
user_guess.append(user_number)
if int(user_number) < random_number:
print("You have to go higher")
elif int(user_number) > random_number:
print("You have to go lower ")
else:
flag = False
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(len(user_guess))
guess()
while True:
play_again = input("Do you like to play again ? : (y/n): ")
if play_again == 'y':
guess()
else:
print("Thank you for playing have a nice day!!! ")
break
python
closed as too broad by Chris_Rands, chepner, usr2564301, msanford, JJJ Nov 14 '18 at 17:56
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
5
With some work, this might be right for Code Review, but you need to be clearer on your aim
– Chris_Rands
Nov 14 '18 at 14:28
Isn't the number supposed to be between 1 and 100?random_number = random.randint(1, 5)
– SilverSlash
Nov 14 '18 at 14:28
@SilverSlash It is easier to test that way that is why it israndom_number = random.randint(1, 5)
– skyn37
Nov 14 '18 at 14:32
add a comment |
I am currently self teaching myself Python , and this is my first program. I am doing some simple projects so i can improve. Would like to hear feedback on this one.
import random
print("There is a number between 1 and 100 guess it and you will win a price : ")
def guess():
# A simple guess game.
random_number = random.randint(1, 5)
flag = True
user_guess =
while flag:
user_number = input("Enter the number : ")
user_guess.append(user_number)
if int(user_number) < random_number:
print("You have to go higher")
elif int(user_number) > random_number:
print("You have to go lower ")
else:
flag = False
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(len(user_guess))
guess()
while True:
play_again = input("Do you like to play again ? : (y/n): ")
if play_again == 'y':
guess()
else:
print("Thank you for playing have a nice day!!! ")
break
python
I am currently self teaching myself Python , and this is my first program. I am doing some simple projects so i can improve. Would like to hear feedback on this one.
import random
print("There is a number between 1 and 100 guess it and you will win a price : ")
def guess():
# A simple guess game.
random_number = random.randint(1, 5)
flag = True
user_guess =
while flag:
user_number = input("Enter the number : ")
user_guess.append(user_number)
if int(user_number) < random_number:
print("You have to go higher")
elif int(user_number) > random_number:
print("You have to go lower ")
else:
flag = False
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(len(user_guess))
guess()
while True:
play_again = input("Do you like to play again ? : (y/n): ")
if play_again == 'y':
guess()
else:
print("Thank you for playing have a nice day!!! ")
break
python
python
edited Nov 14 '18 at 14:33
skyn37
asked Nov 14 '18 at 14:25
skyn37skyn37
413
413
closed as too broad by Chris_Rands, chepner, usr2564301, msanford, JJJ Nov 14 '18 at 17:56
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Chris_Rands, chepner, usr2564301, msanford, JJJ Nov 14 '18 at 17:56
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
5
With some work, this might be right for Code Review, but you need to be clearer on your aim
– Chris_Rands
Nov 14 '18 at 14:28
Isn't the number supposed to be between 1 and 100?random_number = random.randint(1, 5)
– SilverSlash
Nov 14 '18 at 14:28
@SilverSlash It is easier to test that way that is why it israndom_number = random.randint(1, 5)
– skyn37
Nov 14 '18 at 14:32
add a comment |
5
With some work, this might be right for Code Review, but you need to be clearer on your aim
– Chris_Rands
Nov 14 '18 at 14:28
Isn't the number supposed to be between 1 and 100?random_number = random.randint(1, 5)
– SilverSlash
Nov 14 '18 at 14:28
@SilverSlash It is easier to test that way that is why it israndom_number = random.randint(1, 5)
– skyn37
Nov 14 '18 at 14:32
5
5
With some work, this might be right for Code Review, but you need to be clearer on your aim
– Chris_Rands
Nov 14 '18 at 14:28
With some work, this might be right for Code Review, but you need to be clearer on your aim
– Chris_Rands
Nov 14 '18 at 14:28
Isn't the number supposed to be between 1 and 100?
random_number = random.randint(1, 5)
– SilverSlash
Nov 14 '18 at 14:28
Isn't the number supposed to be between 1 and 100?
random_number = random.randint(1, 5)
– SilverSlash
Nov 14 '18 at 14:28
@SilverSlash It is easier to test that way that is why it is
random_number = random.randint(1, 5)
– skyn37
Nov 14 '18 at 14:32
@SilverSlash It is easier to test that way that is why it is
random_number = random.randint(1, 5)
– skyn37
Nov 14 '18 at 14:32
add a comment |
1 Answer
1
active
oldest
votes
- Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.
- Converting
user_number
to integer every time you want to compare it with some other integer is costly, and frivolous. Convert it once and reassign it touser_number
. flag
is not necessary. Usewhile True
, and break when needed.- Keeping an entire
list
object just to count user input is a bit overkill. Have a single integer (count
in my example) and use it. - You print "between 1 and 100" but your program creates a random integer between 1 and 5. I am not sure about your intentions with doing that (perhaps a debugging aid), but I presume it's a small mistake.
- A minor suggestion, move the
print("There is a number between 1 and 100 guess it and you will win a price : ")
to theguess
function since it makes more sense for the function to declare its own purpose.
Here's how I would make those changes to the guess
function:
def guess():
print("There is a number between 1 and 100 guess it and you will win a price : ")
# A simple guess game.
random_number = random.randint(1, 100)
# flag = True obsolete
count = 0
while True:
user_number = input("Enter the number : ")
if (user_number.isdigit()):
user_number = int(user_number)
else:
print("Invalid:")
continue
count += 1
if user_number < random_number:
print("You have to go higher")
elif user_number > random_number:
print("You have to go lower ")
else:
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(count)
break
Thank you andrandom_number = random.randint(1, 5)
was for testing only .
– skyn37
Nov 14 '18 at 20:14
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
- Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.
- Converting
user_number
to integer every time you want to compare it with some other integer is costly, and frivolous. Convert it once and reassign it touser_number
. flag
is not necessary. Usewhile True
, and break when needed.- Keeping an entire
list
object just to count user input is a bit overkill. Have a single integer (count
in my example) and use it. - You print "between 1 and 100" but your program creates a random integer between 1 and 5. I am not sure about your intentions with doing that (perhaps a debugging aid), but I presume it's a small mistake.
- A minor suggestion, move the
print("There is a number between 1 and 100 guess it and you will win a price : ")
to theguess
function since it makes more sense for the function to declare its own purpose.
Here's how I would make those changes to the guess
function:
def guess():
print("There is a number between 1 and 100 guess it and you will win a price : ")
# A simple guess game.
random_number = random.randint(1, 100)
# flag = True obsolete
count = 0
while True:
user_number = input("Enter the number : ")
if (user_number.isdigit()):
user_number = int(user_number)
else:
print("Invalid:")
continue
count += 1
if user_number < random_number:
print("You have to go higher")
elif user_number > random_number:
print("You have to go lower ")
else:
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(count)
break
Thank you andrandom_number = random.randint(1, 5)
was for testing only .
– skyn37
Nov 14 '18 at 20:14
add a comment |
- Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.
- Converting
user_number
to integer every time you want to compare it with some other integer is costly, and frivolous. Convert it once and reassign it touser_number
. flag
is not necessary. Usewhile True
, and break when needed.- Keeping an entire
list
object just to count user input is a bit overkill. Have a single integer (count
in my example) and use it. - You print "between 1 and 100" but your program creates a random integer between 1 and 5. I am not sure about your intentions with doing that (perhaps a debugging aid), but I presume it's a small mistake.
- A minor suggestion, move the
print("There is a number between 1 and 100 guess it and you will win a price : ")
to theguess
function since it makes more sense for the function to declare its own purpose.
Here's how I would make those changes to the guess
function:
def guess():
print("There is a number between 1 and 100 guess it and you will win a price : ")
# A simple guess game.
random_number = random.randint(1, 100)
# flag = True obsolete
count = 0
while True:
user_number = input("Enter the number : ")
if (user_number.isdigit()):
user_number = int(user_number)
else:
print("Invalid:")
continue
count += 1
if user_number < random_number:
print("You have to go higher")
elif user_number > random_number:
print("You have to go lower ")
else:
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(count)
break
Thank you andrandom_number = random.randint(1, 5)
was for testing only .
– skyn37
Nov 14 '18 at 20:14
add a comment |
- Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.
- Converting
user_number
to integer every time you want to compare it with some other integer is costly, and frivolous. Convert it once and reassign it touser_number
. flag
is not necessary. Usewhile True
, and break when needed.- Keeping an entire
list
object just to count user input is a bit overkill. Have a single integer (count
in my example) and use it. - You print "between 1 and 100" but your program creates a random integer between 1 and 5. I am not sure about your intentions with doing that (perhaps a debugging aid), but I presume it's a small mistake.
- A minor suggestion, move the
print("There is a number between 1 and 100 guess it and you will win a price : ")
to theguess
function since it makes more sense for the function to declare its own purpose.
Here's how I would make those changes to the guess
function:
def guess():
print("There is a number between 1 and 100 guess it and you will win a price : ")
# A simple guess game.
random_number = random.randint(1, 100)
# flag = True obsolete
count = 0
while True:
user_number = input("Enter the number : ")
if (user_number.isdigit()):
user_number = int(user_number)
else:
print("Invalid:")
continue
count += 1
if user_number < random_number:
print("You have to go higher")
elif user_number > random_number:
print("You have to go lower ")
else:
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(count)
break
- Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.
- Converting
user_number
to integer every time you want to compare it with some other integer is costly, and frivolous. Convert it once and reassign it touser_number
. flag
is not necessary. Usewhile True
, and break when needed.- Keeping an entire
list
object just to count user input is a bit overkill. Have a single integer (count
in my example) and use it. - You print "between 1 and 100" but your program creates a random integer between 1 and 5. I am not sure about your intentions with doing that (perhaps a debugging aid), but I presume it's a small mistake.
- A minor suggestion, move the
print("There is a number between 1 and 100 guess it and you will win a price : ")
to theguess
function since it makes more sense for the function to declare its own purpose.
Here's how I would make those changes to the guess
function:
def guess():
print("There is a number between 1 and 100 guess it and you will win a price : ")
# A simple guess game.
random_number = random.randint(1, 100)
# flag = True obsolete
count = 0
while True:
user_number = input("Enter the number : ")
if (user_number.isdigit()):
user_number = int(user_number)
else:
print("Invalid:")
continue
count += 1
if user_number < random_number:
print("You have to go higher")
elif user_number > random_number:
print("You have to go lower ")
else:
print("Congrats you win !!! nThe numbers of guesses before you won : ")
print(count)
break
edited Nov 14 '18 at 14:57
answered Nov 14 '18 at 14:47
AyxanAyxan
1,618317
1,618317
Thank you andrandom_number = random.randint(1, 5)
was for testing only .
– skyn37
Nov 14 '18 at 20:14
add a comment |
Thank you andrandom_number = random.randint(1, 5)
was for testing only .
– skyn37
Nov 14 '18 at 20:14
Thank you and
random_number = random.randint(1, 5)
was for testing only .– skyn37
Nov 14 '18 at 20:14
Thank you and
random_number = random.randint(1, 5)
was for testing only .– skyn37
Nov 14 '18 at 20:14
add a comment |
5
With some work, this might be right for Code Review, but you need to be clearer on your aim
– Chris_Rands
Nov 14 '18 at 14:28
Isn't the number supposed to be between 1 and 100?
random_number = random.randint(1, 5)
– SilverSlash
Nov 14 '18 at 14:28
@SilverSlash It is easier to test that way that is why it is
random_number = random.randint(1, 5)
– skyn37
Nov 14 '18 at 14:32