449,049 Members | 1,189 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,049 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 import random   print("\t\t\tWelcome to guess my number game!!!") print("Please enter the number between 1 and 100")   number = random.randint(1, 100) guess = int(input("Take a guess: ")) max_guess = 5 tries = 1       while guess != number:     guess = int(input("Take a guess: "))     tries += 1       if guess > number:         print("Lower...")     elif guess < number:         print("Higher...")     else:         print("Good you got it and it only tooked you ", tries, " tries!")         break       if tries == max_guess:         print("\n\nYou are out of tries")         break           input("\n\nPress Enter to exit")   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")

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 import random   print("\t\t\tWelcome to guess my number game!!!") print("Please enter the number between 1 and 100")   number = random.randint(1, 100) guess = int(input("Take a guess: ")) max_guess = 5 tries = 1   while guess != number:            if guess > number:         print("Lower...")     elif guess < number:         print("Higher...")     else:         print("Good you got it and it only tooked you ", tries, " tries!")         break     guess = int(input("Guess again: "))     tries += 1       if tries == max_guess:         print("\n\nYou are out of tries")         break     input("\n\nPress Enter to exit")   Jul 25 '15 #2

 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 import random   print "Welcome to guess my number game!!!" print "Please enter the number between 1 and 100"   number = random.randint(1, 100) print number max_guess = 5 tries = 0   while True:     guess = int(raw_input("Take a guess: "))     tries += 1     if guess > number:         print "Lower..."     elif guess < number:         print "Higher..."     else:         print "Good you got it and it only tooked you", tries, "tries!"         break     if tries == max_guess:         print "\n\nYou are out of tries"         break   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

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