Elterum
Elterum

Reputation: 1

Invalid conditional statement being run

I am a beginner but I am trying to be ambitious and start to learn by creating a text-based game with varying difficulties. I plan on achieving varying difficulties by making use of random values as the scope of my knowledge in programming is limited.

However, I encountered a problem wherein my elif condition inside the difficulty function is always met even though it's seemingly invalid. I am also open for corrections in my code and also suggestions on what I can learn to improve my project. Thank you!

import time
import math
import random

print("BLAZE")
time.sleep(1)
print("Welcome, user. Please tell me your name.")
name = input("Name: ")
print("Welcome,", name, ".")
time.sleep(3)

def difficulty(): 
    print("Please set your difficulty.")
    print("Difficulty levels:")
    print("1 - Expert difficulty: Countries are enveloped in irreparable poverty and relies on war to gain power.")
    print("2 - Challenging difficulty: Your land must be wary of other countries.")
    print("3 - Easy difficulty: Other countries are pleased to make friends rather than war.")
    diff = input("Difficulty: ")
    while diff != "1" or "2" or "3":
        if diff == "1":
            cf1 = input("Confirm difficulty. Yes or No?")
            if cf1 == "Yes":
                print("Understood.")
                time.sleep(1)
                print("Starting game...")
                start(diff)
            if cf1 == "No":
                difficulty()
        if diff == "2":
            cf2 = input("Confirm difficulty. Yes or No?")
            if cf2 == "Yes":
                print("Understood.")
                time.sleep(1)
                print("Starting game...")
                start(diff)
            if cf2 == "No":
                difficulty()
        if diff == "3":
            cf3 = input("Confirm difficulty. Yes or No?")
            if cf3 == "Yes":
                print("Understood.")
                time.sleep(1)
                print("Starting game...")
                start(diff)
            if cf3 == "No":
                difficulty()
        elif diff != "1" or "2" or "3":
            print("Invalid Answer.")
            difficulty()

def start(diff):
    print("Expert difficulty.")
    print("Good morning.")

def start(diff):
    print("Challenging difficulty.")
    print("Good morning.")

def start(diff):
    print("Easy difficulty.")
    print("Good morning.")

difficulty()

Upvotes: 0

Views: 73

Answers (3)

wallisers
wallisers

Reputation: 388

Your condition for elif is always true. Your asking Python if string "2" or string "3" exist, which they do. You are not asking for the value of the variable.

Try:

>>> if "1":
...     print("True")

and

>>> i = "1"
>>> if i == "1":
...     print("True")

The first condition checks if the string exists, the second one checks for the value of the variable.

Refactoring the whole if-statement, your code should look something like this:

if diff == "1":
    # Your code
elif diff == "2":
    # Your code
elif diff == "3":
    # Your code
else:
    print("Invalid Answer.")
    difficulty()

Upvotes: 0

valeriored83
valeriored83

Reputation: 141

The expression a = diff != "1" or "2" or "3" evaluate always to True since the string "2" and "3" evaluates True. This is because the operator != applies only to the string "1". You should either do:

elif diff != "1" or diff != "2": # no need to check for != "3" because we are in the else

However you can better do a chain of if-elif, so that you check each condition only once.

if diff == "1":
  ...
elif diff == "2":
  ...
elif diff == "3":
  ...
else:
  print("Invalid Answer.")
  difficulty()

Upvotes: 1

ExplodingGayFish
ExplodingGayFish

Reputation: 2897

I believe you want to say elif diff != "1" and diff != "2" and diff != "3":. The problem lies in your or statement. In python if you use a not empty string as a condition it always returns True, the same apply for non empty list and dict as well:

if "2":
    print("not empty string")
if '':
    print("empty string")

if ["aaa"]:
    print("not empty list")
if []:
    print("empty list")

if {"a" : 1}:
    print("not empty dict")
if {}:
    print("empty dict")

Output:

not empty string
not empty list
not empty dict

The or statement means returning True if any of condition satisfies.

Upvotes: 0

Related Questions