Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with if vs elif
#1
Hello World,

I am going through this book 'Automate the Boring Stuff with Python' and came across the secret number game example (example here: https://autbor.com/guessthenumber/). I decided to code my own game but used the if statement instead of the elif and the programme does not run in the loop and proceeds to the last line of code instead. Any thoughts why? Appreciate the time lads and lasses.


# This is a guess the number game.
2	import random
3	secretNumber = random.randint(1, 20)
4	print('I am thinking of a number between 1 and 20.')
5	
6	# Ask the player to guess 6 times.
7	for guessesTaken in range(1, 7):
8	    print('Take a guess.')
9	    guess = int(input())
10	
11	    if guess < secretNumber:
12	        print('Your guess is too low.')
13	    elif guess > secretNumber:
14	        print('Your guess is too high.')
15	    else:
16	        break    # This condition is the correct guess!
17	
18	if guess == secretNumber:
19	    print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
20	else:
21	    print('Nope. The number I was thinking of was ' + str(secretNumber))
buran write Jan-07-2021, 06:18 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.

Don't include line numbers in the text you post.
Reply
#2
I see that you copied and pasted the code from this site which we do not know. Can you post your code instead by using bbcode tags as explained here BBCode? Please include any error or output message that makes you think that the code does not work properly.
Reply
#3
I hate whatever it is you use to edit your code. To run your example I had to delete the numbers that appear at the start of each line. If you can post without doing that, please do so in the future. Thankfully the program is only 21 lines and it didn't take long to get the code ready to run. And then it ran without issue.

So what is the question? Normally people ask questions about code that doesn't work, and the code they post exhibits the problem. Maybe you should try that.
Reply
#4
Sorry I am new to this forum, I hope this is clearer.

why doesn't this code work if the player guesses less than the secret random integer? If the player guesses more the 'for' loop continues but breaks when the player guesses less.

import random
secretNumber = random.randint(1, 20)
     print('I am thinking of a number between 1 and 20.')

# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):
     print('Take a guess.')
     guess = int(input())

     if guess < secretNumber:
          print('Your guess is too low.')
     if guess > secretNumber:
          print('Your guess is too high.')
     else:
          break    # This condition is the correct guess!
	
if guess == secretNumber:
     print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
     print('Nope. The number I was thinking of was ' + str(secretNumber))
The original code used elif statement and it works. Does it have to do with the flow?

import random
secretNumber = random.randint(1, 20)
     print('I am thinking of a number between 1 and 20.')

# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):
     print('Take a guess.')
     guess = int(input())

     if guess < secretNumber:
          print('Your guess is too low.')
     elif guess > secretNumber:
          print('Your guess is too high.')
     else:
          break    # This condition is the correct guess!
	
if guess == secretNumber:
     print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
     print('Nope. The number I was thinking of was ' + str(secretNumber))
Reply
#5
What happens in your code when your guess is less than the correct number?
On lines 10-11 it checks, condition is True and it prints message.
Then on lines 12-15 it checks the if condition, which is False, then it moves to else part and breaks out no matter what, incl. when your guess is less than correct.

What happens in original code when your guess is less than the correct number?
On lines 10-11 it checks, condition is True and it prints the message and continues with next iteration of the loop. Lines 12-15 are never executed.

You can use debug tools to follow the execution and debug such problems. You can even do it by hand, walking down the code line by line and determine what will happen next.

You can visualise your code execution step by step here:
http://pythontutor.com/visualize.html#mode=edit
print_hello_world likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Thank you for the help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,217 Jul-28-2019, 05:52 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020