By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Guess my number problem

P: 4
I have a problem. For me all is correct but when im trying to have a first guess cmd dont print "print("Lower...")" and neither "print("Higher...")" statement. after first try everything works good. Help me just to find a problem because i cant see anything. Thank You

Here is my source code:

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. print("\t\t\tWelcome to guess my number game!!!")
  4. print("Please enter the number between 1 and 100")
  5.  
  6. number = random.randint(1, 100)
  7. guess = int(input("Take a guess: "))
  8. max_guess = 5
  9. tries = 1
  10.  
  11.  
  12.  
  13. while guess != number:
  14.     guess = int(input("Take a guess: "))
  15.     tries += 1
  16.  
  17.     if guess > number:
  18.         print("Lower...")
  19.     elif guess < number:
  20.         print("Higher...")
  21.     else:
  22.         print("Good you got it and it only tooked you ", tries, " tries!")
  23.         break
  24.  
  25.     if tries == max_guess:
  26.         print("\n\nYou are out of tries")
  27.         break
  28.  
  29.  
  30.  
  31.  
  32.  
  33. input("\n\nPress Enter to exit")
  34.  
Jul 25 '15 #1

✓ answered by bvdet

Move your first guess under the while statement. Initialize tries to 0. I modified your code for Python 2.7:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. print "Welcome to guess my number game!!!"
  4. print "Please enter the number between 1 and 100"
  5.  
  6. number = random.randint(1, 100)
  7. print number
  8. max_guess = 5
  9. tries = 0
  10.  
  11. while True:
  12.     guess = int(raw_input("Take a guess: "))
  13.     tries += 1
  14.     if guess > number:
  15.         print "Lower..."
  16.     elif guess < number:
  17.         print "Higher..."
  18.     else:
  19.         print "Good you got it and it only tooked you", tries, "tries!"
  20.         break
  21.     if tries == max_guess:
  22.         print "\n\nYou are out of tries"
  23.         break
  24.  
  25. raw_input("\n\nPress Enter to exit")

Share this Question
Share on Google+
4 Replies


P: 4
After changing " tries += 1 guess = int(input("Guess again: "))" from the beggining to the end of the loop it works ok but after you guess the number cmd doesnt want to print "print("Good you got it and it only tooked you ", tries, " tries!")". Maybe this code is a mess but im new in this. Just need some more experience. Thanks for help.
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. print("\t\t\tWelcome to guess my number game!!!")
  4. print("Please enter the number between 1 and 100")
  5.  
  6. number = random.randint(1, 100)
  7. guess = int(input("Take a guess: "))
  8. max_guess = 5
  9. tries = 1
  10.  
  11. while guess != number:       
  12.     if guess > number:
  13.         print("Lower...")
  14.     elif guess < number:
  15.         print("Higher...")
  16.     else:
  17.         print("Good you got it and it only tooked you ", tries, " tries!")
  18.         break
  19.     guess = int(input("Guess again: "))
  20.     tries += 1
  21.  
  22.     if tries == max_guess:
  23.         print("\n\nYou are out of tries")
  24.         break
  25.  
  26.  
  27. input("\n\nPress Enter to exit")
  28.  
Jul 25 '15 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Move your first guess under the while statement. Initialize tries to 0. I modified your code for Python 2.7:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. print "Welcome to guess my number game!!!"
  4. print "Please enter the number between 1 and 100"
  5.  
  6. number = random.randint(1, 100)
  7. print number
  8. max_guess = 5
  9. tries = 0
  10.  
  11. while True:
  12.     guess = int(raw_input("Take a guess: "))
  13.     tries += 1
  14.     if guess > number:
  15.         print "Lower..."
  16.     elif guess < number:
  17.         print "Higher..."
  18.     else:
  19.         print "Good you got it and it only tooked you", tries, "tries!"
  20.         break
  21.     if tries == max_guess:
  22.         print "\n\nYou are out of tries"
  23.         break
  24.  
  25. raw_input("\n\nPress Enter to exit")
Jul 26 '15 #3

P: 4
Thanks, I using Python 3.4.3 but thank you. It was so easy that I feel stupid right now
Jul 26 '15 #4

bvdet
Expert Mod 2.5K+
P: 2,851
You are welcome. Sometimes the obvious solution is the hardest to see.
Jul 26 '15 #5

Post your reply

Sign in to post your reply or Sign up for a free account.