Python Forum
if statement not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: if statement not working (/thread-29480.html)



if statement not working - g0g0g1g - Sep-04-2020

if i enter random gibberish it still runs the code as if i entered one of the acceptable names I.E. my code carries out the first if statement, despite it not being a correct name why isn't it saying access denied when i'm not saying the correct name. Wall Think Huh

import random

#round_score is score for that round
#total_score is the running total
#total is the total of both dice added

player_name = input()

if player_name == "Alex Johnson" or "Bob Johnson" or "Duncan Johnson" or "Joan Stokes": 
    print ("Die 1 Die 2 Total Die 3 Round score")

    die_1 = random. randint(1,6)
    die_2 = random. randint(1,6)
    total = (die_1) + (die_2)

    total_2 = 0

    if die_1 == die_2:
        die_3 = random. randint(1,6)
        total_2 = die_3 + total_2

    #if total == 2 or total == 4 or total == 6 or total == 8 or total == 10 or total == 12:
    if total%2 == 0:
        round_score = total + 10 + total_2
    else:
        round_score = total - 5 + total_2

    print (" ", die_1, "   ", die_2,"   ", total,"   ", total_2,"     ", round_score)

    print (player_name)

elif player_name == "Merlin" or "Poppy":
    print ("nice try dog")

#my dogs

else:
    print ("Access denied")
edit: in my post i have indents but it wont show up


RE: if statement not working - perfringo - Sep-04-2020

You should think what this line does (hint: it will be always True):

if player_name == "Alex Johnson" or "Bob Johnson" or "Duncan Johnson" or "Joan Stokes":
Probably you want something like:

if player_name in ("Alex Johnson", "Bob Johnson", "Duncan Johnson", "Joan Stokes"):



RE: if statement not working - nilamo - Sep-08-2020

For more info: https://python-forum.io/Thread-Multiple-expressions-with-or-keyword