0

Here is the problem:

You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday on that day, your speed can be 5 higher in cases.

My code:

speed = int(input());
birthday = input();

if (not birthday):
    if (speed <= 60):
        print(0);
    elif (speed >= 61 and speed <= 80):
        print(1);
    elif (speed >= 81):
        print(2);
elif (birthday):
    if (speed >= 61 and speed <= 65):
        print(0);
    elif (speed >= 81 and speed <= 85):
        print(1);

And this is the output:

What am I doing wrong?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Scottie Joe
  • 97
  • 1
  • 10
  • It's not directly related to your errors, but you're adding a lot of unnecessary punctuation to your code. Python doesn't require parentheses around the condition in an `if` or `elif` statement, and it doesn't require semicolons at the end of lines. – Blckknght Mar 07 '16 at 04:27
  • Please don't post textual info as an image. – roadrunner66 Mar 07 '16 at 04:28
  • Are you running python 2 or 3? – tdelaney Mar 07 '16 at 05:53

4 Answers4

4

When you receive input with input(), what you get back is not a boolean, as you assume here, but rather a string. Thus, you should be checking

if (birthday == "True")

and

elif (birthday == "False")
Erik Godard
  • 5,930
  • 6
  • 30
  • 33
0

Currently birthday is a string, so when you say if not birthday, Python literally checks to make sure the string exists, and when it does, it runs the elif statement. You need to say something like birthday = bool(input()), and have the user input a 0 or a 1. Also, on unrelated notes Python does not need semicolons at the end of each statement - actually they are very frowned upon. Also, have something inside your inputs, like speed = int(input("How fast were you driving?: ")) so that the user doesn't have to magically know what they're putting in. Also, the parenthesis in your if statements are unnecessary as well. Finally, instead of saying elif birthday:, you should just say else:, because obviously if it's not false, then it's true.

MANA624
  • 986
  • 4
  • 10
  • 34
  • it's not true that Python checks whether the string "exists." it checks whether the string is *empty*. – abcd Mar 07 '16 at 04:28
  • I fail to see the difference, but thank you for the clarification. – MANA624 Mar 07 '16 at 04:29
  • an empty string still exists as a string. determining whether a string exists would amount to determining whether a string variable has been defined. this is *not* what the `if` statement does. – abcd Mar 07 '16 at 04:31
  • 2
    to check whether the string exists, you would do `if 'birthday' in locals():`, not `if birthday:`. see [this question](http://stackoverflow.com/q/843277/2623899). – abcd Mar 07 '16 at 04:37
0

The easiest way is to subtract 5 from the speed if it is the birthday.

birthdayValue = {True: 5, False: 0}
speed = int(input("Speed: "))
birthday = input("Birthday (True/False): ")
speed -= birthdayValue[birthday == 'True']
if speed <= 60:
    print(0)
elif 60 < speed <= 80:
    print(1)
elif 80 < speed <= 85:
    print(2)
Goodies
  • 4,439
  • 3
  • 31
  • 57
0

In python 3, input returns a string which you should validate before accepting. For birthday, you need to decide how strict you are about what is entered and write the code to enforce it. If the input is "True" or "False" but you are okay with people messing up case or adding some spaces, then this would do

speed = int(input())
birthday = input().strip().lower()

# valiate input
if speed < 0:
    print("Speed must be > 0")
    exit(1)
if birthday == 'true':
    birthday = True
elif birthday == 'false':
    birthday = False
else:
    print("Birthday must be True or False")
    exit(1)

# give 'em a break on their birthday
if birthday:
    speed -= 5

# calculate points
if (speed <= 60):
    print(0)
elif (speed >= 61 and speed <= 80):
    print(1)
else:
    print(2)
tdelaney
  • 73,364
  • 6
  • 83
  • 116