How can i improve this? [closed]










-4















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









share|improve this 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 is random_number = random.randint(1, 5)

    – skyn37
    Nov 14 '18 at 14:32















-4















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









share|improve this 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 is random_number = random.randint(1, 5)

    – skyn37
    Nov 14 '18 at 14:32













-4












-4








-4








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 is random_number = random.randint(1, 5)

    – skyn37
    Nov 14 '18 at 14:32












  • 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







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












1 Answer
1






active

oldest

votes


















2














  1. Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.

  2. 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 to user_number.


  3. flag is not necessary. Use while True, and break when needed.

  4. 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.

  5. 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.

  6. A minor suggestion, move the print("There is a number between 1 and 100 guess it and you will win a price : ") to the guess 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





share|improve this answer

























  • Thank you and random_number = random.randint(1, 5) was for testing only .

    – skyn37
    Nov 14 '18 at 20:14


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














  1. Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.

  2. 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 to user_number.


  3. flag is not necessary. Use while True, and break when needed.

  4. 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.

  5. 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.

  6. A minor suggestion, move the print("There is a number between 1 and 100 guess it and you will win a price : ") to the guess 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





share|improve this answer

























  • Thank you and random_number = random.randint(1, 5) was for testing only .

    – skyn37
    Nov 14 '18 at 20:14
















2














  1. Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.

  2. 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 to user_number.


  3. flag is not necessary. Use while True, and break when needed.

  4. 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.

  5. 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.

  6. A minor suggestion, move the print("There is a number between 1 and 100 guess it and you will win a price : ") to the guess 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





share|improve this answer

























  • Thank you and random_number = random.randint(1, 5) was for testing only .

    – skyn37
    Nov 14 '18 at 20:14














2












2








2







  1. Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.

  2. 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 to user_number.


  3. flag is not necessary. Use while True, and break when needed.

  4. 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.

  5. 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.

  6. A minor suggestion, move the print("There is a number between 1 and 100 guess it and you will win a price : ") to the guess 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





share|improve this answer















  1. Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.

  2. 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 to user_number.


  3. flag is not necessary. Use while True, and break when needed.

  4. 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.

  5. 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.

  6. A minor suggestion, move the print("There is a number between 1 and 100 guess it and you will win a price : ") to the guess 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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 14:57

























answered Nov 14 '18 at 14:47









AyxanAyxan

1,618317




1,618317












  • 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

















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






Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo