0

Trying to get my if to either allow the user to input a name or randomly choose but no matter what is entered it goes to the input of name?

Any ideas?

Here is my snippet:

import random
import os
names = "Balo", "Bandugl", "Baroro", "Cag", "Charoth", "Dugling", "Dulko", "Fangot", "Gormath", "Varth", "Ugort", "Ogong", "Tuli", "Corg", "Chau", "Korg", "Salath", "Wegia",        "Wecha", "Moroth", "Kangir", "Bethindu", "Duak", "Fagoot", "Penam"
rand_name = random.choice(names)

#Character creation (1)#
if input("Would you like to choose a name?: ") == "Y" or "y" or "yes" or "YES":
    print("")
    c1 = {"Name":input("Name: ")}
else:
    c1 = {"Name":rand_name}
print(c1)

Thanks in advance!

FallenAngel
  • 17,186
  • 14
  • 82
  • 110
Aidan H
  • 21
  • 2

4 Answers4

3

In your line

if input("Would you like to choose a name?: ") == "Y" or "y" or "yes" or "YES":

You can not chain values like that, it will evaluated as follows

( input("Would you like to choose a name?: ") == "Y") or ("y") or ("yes") or ("YES")

in which case, or ("y") will return True since non-empty strings are all evaluated True

You must try:

if input("Would you like to choose a name?: ") in ["Y", "y", "yes", "YES"]:

then it will check if input is one of "Y", "y", "yes" or "YES" values

Or you can use str.upper() to make your option list less crowded:

if input("Would you like to choose a name?: ").upper() in ["Y", "YES"]:
FallenAngel
  • 17,186
  • 14
  • 82
  • 110
1
if input("Would you like to choose a name?: ").lower() in ("y", "yes"):
sloth
  • 95,484
  • 19
  • 164
  • 210
Django Doctor
  • 8,450
  • 9
  • 45
  • 66
0

You comparison is wrong.

if input("Would you like to choose a name?: ") in ("Y", "y", "yes", "YES"):

To understand your error:

>>> "a" == "Y" or "y"
'y'
>>> "Y" == "Y" or "y"
True
iurisilvio
  • 4,727
  • 1
  • 26
  • 31
0

Use raw_input(2.7) input in 3

import random
import os
names = "Balo", "Bandugl", "Baroro", "Cag", "Charoth", "Dugling", "Dulko", "Fangot", "Gormath", "Varth", "Ugort", "Ogong", "Tuli", "Corg", "Chau", "Korg", "Salath", "Wegia",        "Wecha", "Moroth", "Kangir", "Bethindu", "Duak", "Fagoot", "Penam"
rand_name = random.choice(names)

#Character creation (1)#
if raw_input('Enter your name: ').upper() in ["Y" , "YES" ]:
    print("")

    c1 = {"Name":raw_input("Name: ")}
else:
    c1 = {"Name":rand_name}
print(c1)
Abhilash Joseph
  • 1,166
  • 1
  • 14
  • 28