1

So I'm currently developing a score keeper for a quiz game I'm making.

score = 0

bolivia = str(input("Name a capital of Bolivia."))

if (bolivia == "La Paz" or "Sucre"):

    print ("Correct")

    score = score + 50

    print (score)

else:

    print ("You/re incorrect")

input ("Press enter to exit;)")

The problem is that if I get the answer wrong it prints this:

Correct
50
Press enter to exit;)

I don't know what I'm doing wrong. Any feed back is appreciated.

4 Answers4

3

The statement

if (bolivia == "La Paz" or "Sucre"):

Has the following two statements:

1) bolivia == "La Paz"

2) "Sucre"

The latter statement "Sucre" is a string which is a Truthy value, thus the if statement as a whole will pass as they are joined together with the or keyword.

What you likely want to evaluate is

if bolivia == "La Paz" or bolivia == "Sucre":

Or consider the in keyword:

if bolivia in ("La Paz", "Sucre"):
metatoaster
  • 17,419
  • 5
  • 55
  • 66
1

The problem with your source code is the way you construct the if statement.

You should edit it to look like this:

if bolivia == "La Paz" or bolivia == "Sucre":

Notice that you should use the bolivia variable again.

Rieljun Liguid
  • 1,511
  • 1
  • 10
  • 17
0

The problem is with the line if (bolivia == "La Paz" or "Sucre"): or "Sucre" will always evaluate to true.

You need to check if (bolivia == "La Paz" or bolivia == "Sucre"):

ktzr
  • 1,625
  • 1
  • 11
  • 25
0

Firstly the issue you have with your statement is that when you perform boolean logic (and/or statements here) is that it is evaluated as an expression.

Take the third line in which you have: if (bolivia == "La Paz" or "Sucre"):

Here you have the boolean logic operator or. This means it will evaluate each side (or rather is will evaluate the first side.. if that is correct it ignores the second statement.. but that is just a tidbit of extra information).

bolvia == "La Paz" is a valid expression. You are asking if bolvia is equal to the string "La Paz".

"Sucre" is not a valid statement. It has no meaning, you must form this into an expression also.

bolvia == "La Paz" or bolvia == "Sucre" would be two correct expressions that could be evaluated by an or statement.

Now you should have the intended behaviour of your if statement.

I would recommend making this more foolproof also by making it case-insensitive. By this I mean you could change the string into the proper format before testing. This would allow for "sucre", "la paz" and other possibilities.

This would change your initial statement to

bolivia = str(input("Name a capital of Bolivia.")).title()

Now in your test statements you could enter la paz and then receive La Paz as an output.

score = score + 50 can also be shortened to score += 50 as this is a shorthand for the former.

This would change your code to the following:

    score = 0
    bolivia = str(input("Name a capital of Bolivia.")).title()

    if (bolivia == "La Paz" or bolivia == "Sucre"):
        print ("Correct")
        score += 50
        print (score)
    else:
        print ("You/re incorrect")
        input ("Press enter to exit;)")

Hope this helps.