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


Messages In This Thread
Help with if vs elif - by print_hello_world - Jan-07-2021, 06:09 AM
RE: Help with if vs elif - by Gribouillis - Jan-07-2021, 06:23 AM
RE: Help with if vs elif - by deanhystad - Jan-07-2021, 06:30 AM
RE: Help with if vs elif - by print_hello_world - Jan-07-2021, 07:37 AM
RE: Help with if vs elif - by buran - Jan-07-2021, 07:51 AM
RE: Help with if vs elif - by print_hello_world - Jan-07-2021, 08:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,266 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