Python Forum
if statement not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if statement not working
#1
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
Reply
#2
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"):
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
For more info: https://python-forum.io/Thread-Multiple-...or-keyword
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Else Statement Not Working SenkouSimmer 4 3,246 Jul-22-2019, 11:42 AM
Last Post: jefsummers
  Simple IF statement not working as intended gortexxx 2 2,787 May-17-2018, 07:54 PM
Last Post: gortexxx
  help! if statement not working molliemae 2 2,594 Apr-26-2018, 11:13 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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