473,383 Members | 1,862 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,383 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 1714
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.