473,320 Members | 1,820 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,320 software developers and data experts.

Infinite loop?

3
I am a beginner to Python and am just trying to make a simple 'Guess the number' game. I programmed it to tell you weather the number you guessed is higher or lower than the actual number. However, the 'Higher' and 'Lower' appear on the screen a couple of hundred times instead of just one.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import random
  3.  
  4. print('Welcome to Guess the Number')
  5. print('Try to guess the number in as few tries as possible')
  6.  
  7. the_number= random.randint(1, 100)
  8.  
  9. guess=int(input("Take a guess!  "))
  10. tries=1
  11.  
  12.  
  13. while guess != the_number:
  14.           if guess>the_number:
  15.               print('Lower... ')
  16.  
  17.  
  18. else:
  19.           print('Higher... ')
  20.  
  21.  
  22. guess= int(input('Take a guess!  '))
  23. tries+=1
  24.  
  25. print('Well done! The number was', the_number)
  26. print('and it only took u=you', tries, 'tries!')
  27.  
  28. input('n/nPress the enter key to exit')
  29.  
Attached Images
File Type: jpg pythpn.jpg (10.2 KB, 159 views)
Sep 23 '15 #1
1 1640
dwblas
626 Expert 512MB
Your indentation says that the only thing that happens under the while loop is the if statement. Everything from the "else" on happens after the while loop exits, including the input, so "guess" never changes=an infinite loop. And what happens when they don't guess it in the number of tries, or enter anything other than a number.
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. print('Welcome to Guess the Number between 1 and 100')
  4. print('Try to guess the number in as few tries as possible')
  5.  
  6. the_number= random.randint(1, 100)
  7.  
  8. max_tries=10 
  9. tries=0
  10. guess = -1
  11.  
  12. ## start while 
  13. while guess != the_number and tries < max_tries:
  14.     guess= int(input('Take a guess!  '))
  15.     tries+=1
  16.     if guess == the_number:
  17.         print('Correct')
  18.     elif guess > the_number:
  19.         print('Number Is Lower... ')
  20.     else:
  21.         print('Number Is Higher... ')
  22. ## end of while loop
  23.  
  24. print('Well done! The number was', the_number)
  25. print('and it only took u=you', tries, 'tries!')
  26.  
  27. input('\n\nPress the enter key to exit')  ## enter to exit is always funny 
Sep 23 '15 #2

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

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
4
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.