-2

I am making a text adventure game, but it keeps running the 1st variation in the code, even though I specified both scenarios:

print("By Rishi Suresh 2021")
print('')
print('')
print("A   S h o r t   R i d e")
print('')
print('')
print("    P L A Y   ( P )")
print('')
print('')
if input()=="p":
  print('')
  print('')
  print("Your journey begins on a bleak day. It's cloudy, and the windows of your apartment amplify the pitter patter of the soft rain outside. Your clothes are strewn about. You hear the landline ringing. You also see a diary.")
  print('')
  print('')
  if input("What do you do? Do you pick up the diary (A) or answer the landline (B)? ")=="b"or "B":
    print("")
    print("")
    print("You pick up the landline, and hear a voice on the other end. It is scratchy, and tells you to come to the London Underground. You pick up your clothes, a empty whiskey bottle and some glasses, and dress. You head outside. ")
  elif input() == "A" or "a":
    print("The diary reads:")
    print('')
    print('')
    print("Dear Diary,")
    print("Tommorow's the day. We have all the trucks ready, all the Tommy's, all the big men. This stop and shop will be the sucess of the century.")
    print("The landline rings again. You pick up the landline, and hear a voice on the other end. It is scratchy, and tells you to come to the London Underground. You pick up your clothes, a empty whiskey bottle and some glasses, and dress. You head outside.")

2 Answers2

1

This clause:

  if input("What do you do? Do you pick up the diary (A) or answer the landline (B)? ")=="b"or "B":

will always pass, because "B" (and "A" or any non-empty string, for that matter) is a truthy value:

if "B":
    print("Hey!")

if "A":
    print("I pass, too!")

if "A really, really, really long string that goes on...":
    print("So do I!")

if "":
    print("I'll never print.")

This means if <anything> or "B": will always pass (provided evaluation of <anything> doesn't throw an exception or change control flow), because the second part of the or is satisfied. The == operator is binding to input (i.e. input(...) == "b" or "B" is (input(...) == "b") or "B"); you can't express "the result is a member of some set" in this way (i.e. x == 1 or 2 is (x == 1) or 2, not x in (1, 2)).

Assign the result of your input calls to some variable result, and then test result in ("b", "B") or result.lower() == "b" (ditto for the second case).

crcvd
  • 1,465
  • 1
  • 12
  • 23
  • As an aside: rather than using `print('')` to generate newlines, just append them to your string (i.e. `print("Add two newlines\n\n")`). – crcvd Mar 16 '21 at 22:07
0

by asking for another input in the elif you are asking for another user input discarding the user's previous input,plus or "a" would cause the statment to always be true since you are not checking if the input is equal to "a", this would be better:

print("")
print('')
print('')
print("")
print('')
print('')
print(" ")
print('')
print('')
if input()=="p":
  print('')
  print('')
  print("")
  print('')
  print('')
  option=input(" a or b")
  if option =="b"or option =="B":
    print("")
    print("")
    print("")
  elif option == "A" or option == "a":
    print("The diary reads:")
    print('')
    print('')
    print("Dear Diary,")
    print("")
    print("")
paulko
  • 21
  • 2