473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Guess my number problem

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")

4 1715
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
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
You are welcome. Sometimes the obvious solution is the hardest to see.
Jul 26 '15 #5

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

Similar topics

1
by: dave | last post by:
I have one auto number field in one table in sql server 2000. I'm inserting new record using insert statement and am not giving any value for that field as its auto number. On the very next...
5
by: fatih kayaalp | last post by:
Hi, i have imported an access database into sqlserver 2000. In access, i have some table fields which were using automatic number as datatype. But i see that there is not a datatype in sqlserver...
16
by: nicolas | last post by:
Aim: scanf a number what random digit,split this number; for example: input "5680" output "5,6,8,0"; input "45" output "4,5";
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
3
by: cathf | last post by:
I am working on a program where the user guesses a random number generated in VB. The numbers entered by the user must be within the range 1 to 20, so the program has been validated. All valid...
5
by: arali | last post by:
I have builed a database in access and using forms of Visual Basic6 The problem is this I have a field Id which is of type long and autoincrement Now when i create a form and run and type my...
16
by: istillshine | last post by:
In my code I used NAN and isnan(x). I found they were convenient to use. I also noticed that older C standard does not support NAN and isnan(x). When I compiled my program using: gcc -Wall...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.