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!

Mp0int
  • 18,172
  • 15
  • 83
  • 114
Aidan H
  • 21
  • 2
  • Sorted changed my if to if input(... is IN a list of accepted answers variable :) – Aidan H Feb 07 '14 at 12:49
  • @FallenAngel: the system won't let you spell "statement" correctly because a question with that title already exists so... just spell it incorrectly instead? – Wooble Feb 07 '14 at 12:56
  • My bad, a mis-spelling. Re-edited for a better title. – Mp0int Feb 07 '14 at 13:00

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"]:
Mp0int
  • 18,172
  • 15
  • 83
  • 114
1
if input("Would you like to choose a name?: ").lower() in ("y", "yes"):
sloth
  • 99,095
  • 21
  • 171
  • 219
Rich Tier
  • 9,021
  • 10
  • 48
  • 71
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,868
  • 1
  • 30
  • 36
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,196
  • 1
  • 14
  • 28