Trying to replace input string with another string with multiple conditions

Python noob here! I’m trying to replace an input string with another string, depending on which condition is met. My problem is that it only works when the first condition is met. When I run the program it prompts me for an input of A, B, C, D or E. If I enter “a” or “A” the string gets converted to “Class A”, which shows when the print statement is executed. If I enter any other option (“b” thru “e” or “B” thru “E”) the string never gets converted. My print statement prints the original input string, not the desired replacement string. Been beating my head against the keyboard for 4 hours now. What am I doing wrong?

rv_class = [“Class A”, “Class C”, “Class B/Van”, “Travel Trailer”, “5th Wheel” ]

a = rv_class[0]
b = rv_class[1]
c = rv_class[2]
t = rv_class[3]
f = rv_class[4]

rv_class[0] = “Class A”
rv_class[1] = “Class B”
rv_class[2] = “Class C”
rv_class[3] = “Travel Trailer”
rv_class[4] = “5t Wheel”

rv_type = input("Please select your RV Type: \nA) Class A \nB) Class B/Van \nC) Class C \nD) Travel Trailer \nE) 5th Wheel \n\n[A/B/C/D/E]? : ").upper()

if input == “a” or “A”:
newStr = rv_type.replace(“A”, rv_class[0])`
elif input == “b” or “B”:
newStr = rv_type.replace(“B”, rv_class[1])
elif input == “c” or “C”:
newStr = rv_type.replace(“C”, rv_class[2])
elif input == “d” or “D”:
newStr = rv_type.replace(“D”, rv_class[3])
elif input == “e” or “E”:
newStr = rv_type.replace(“E”, rv_class[4])

print("Equipment Type: ", newStr)

This seems to be a very common initial mistake for new users of Python.

You have an if-statment like this:

if input == "a" or "A":
    newStr = rv\_type.replace("A", rv\_class\[0\])`

This is parsed as:

if (input == "a") or "A":

That condition will always be true, regardless of the value. You
actually want one of these two forms (the second if often preferred):

if input == "a" or input == "A":

if input in ("a", "A'):

broadly, logical connections such as and or or group less tightly
than comparisons such as ==, which in turn group less tightly than
other operators such as arithmetic. When you need a different grouping
using brakcets, just as you would in arithmetic. For example:

3 + 5 * 8 == 43     # 3 + (5 * 8)
(3 + 5) * 8 == 64   # (3 + 5) * 8

Also, the name input is the builtin function input() which you used
to read a string from the user. You actually want rv_type, which is
the variable storing the result of your call to input().

Finally, you don’t want any of these backslashes:

newStr = rv\_type.replace("B", rv\_class\[1\])

Just write:

newStr = rv_type.replace("B", rv_class[1])

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Hi Cameron,

Thank you for the response and recommended changes. I think I implemented what you suggested, but still not working. Now when I enter any selection A thru E it shows the string replacement in the output.

Please select your RV Type:
A) Class A
B) Class B/Van
C) Class C
D) Travel Trailer
E) 5th Wheel

[A/B/C/D/E]? : a
A


Here’s my code:

#List function
rv_class = [“Class A”, “Class C”, “Class B/Van”, “Travel Trailer”, “5th Wheel” ]

#Variable for each RV class
a = rv_class[0]
b = rv_class[1]
c = rv_class[2]
t = rv_class[3]
f = rv_class[4]

#Indexed positions [0-4] mapped to their respective string
rv_class[0] = “Class A”
rv_class[1] = “Class B”
rv_class[2] = “Class C”
rv_class[3] = “Travel Trailer”
rv_class[4] = “5t Wheel”

rv_type = input("Please select your RV Type: \nA) Class A \nB) Class B/Van \nC) Class C \nD) Travel Trailer \nE) 5th Wheel \n\n[A/B/C/D/E]? : ").upper()
if input(rv_type) in (“a” , “A”):
newStr = rv_type.replace(“A”, rv_class[0])
elif input(rv_type) in (“b” , “B”):
newStr = rv_type.replace(“B”, rv_class[1])
elif input(rv_type) in (“c” , “C”):
newStr = rv_type.replace(“C”, rv_class[2])
elif input(rv_type) in (“d” , “D”):
newStr = rv_type.replace(“D”, rv_class[3])
elif input(rv_type) in (“e” , “E”):
newStr = rv_type.replace(“E”, rv_class[4])

print("Equipment Type: " + newStr)

Hello @crypto_rayray, I also new to python and programming in general, I will try to help:

In your code:

# List function
rv_class = ["Class A", "Class C", "Class B/Van", "Travel Trailer", "5th Wheel"]

# Variable for each RV class
a = rv_class[0]
b = rv_class[1]
c = rv_class[2]
t = rv_class[3]
f = rv_class[4]

# Indexed positions [0-4] mapped to their respective string
rv_class[0] = "Class A"
rv_class[1] = "Class B"
rv_class[2] = "Class C"
rv_class[3] = "Travel Trailer"
rv_class[4] = "5t Wheel"

First, If you make list of car type in rv_class you dont need to assign to variabel for each RV Class element in rv_class list too be able to call it in your code, you can just call it by the index like below:

# list of RV Class
rv_class = ["Class A", "Class C", "Class B/Van", "Travel Trailer", "5th Wheel"]

print(rv_class[1])

# Output
'Class C'

Second, you also dont need to re-assign value to each index if your list doesn’t need to change;

# Indexed positions [0-4] mapped to their respective string
rv_class[0] = "Class A"
rv_class[1] = "Class B"
rv_class[2] = "Class C"
rv_class[3] = "Travel Trailer"
rv_class[4] = "5t Wheel"

In your code you just re-assign rv_class[0] with string "Class A". If you run your code, value of rv_class[0] just change to "Class A" to "Class A" you change something with the something the same. Example:

fruit = ["banana", "apple"]
print(fruit[0]) # the output will be 'banana' here
fruit[0] = "banana"
print(fruit[0]) # the output will be also 'banana' here
fruit[0] = "watermelon"
print(fruit[0]) # the output will be 'watermelon' here, because you re-assign different value

maybe you need to simplify your code:

# list of RV Car
rv_class = ["Class A", "Class B/Van", "Class C", "Travel Trailer", "5th Wheel"]

# Input from user
rv_type = input(
    "Please select your RV Type: \nA) Class A \nB) Class B/Van \nC) Class C \nD) Travel Trailer \nE) 5th Wheel \n\n[A/B/C/D/E]? : "
).upper()

# make function to help print the choice to user
def show_rv_type(choice):
    print("Equipment Type: " + choice)

# in if condition you don't need to compere `rv_type` value "a" character (lowercase character),
# because you use `.upper()` method so rv_type always will be uppercase character
if rv_type in ("A"):
    newStr = rv_class[0]
    show_rv_type(newStr)
elif rv_type in ("B"):
    newStr = rv_class[1]
    show_rv_type(newStr)
elif rv_type in ("C"):
    newStr = rv_class[2]
    show_rv_type(newStr)
elif rv_type in ("D"):
    newStr = rv_class[3]
    show_rv_type(newStr)
elif rv_type in ("E"):
    newStr = rv_class[4]
    show_rv_type(newStr)
else: # If user input character outside the choice it will be prompt user with "Invalid Input"
    print("Invalid Input!")

Hope it will help, English is not first language and I still learning :+1:

ref:

In discourse you can use markdown code-fence to highlight you code, use </> toolbar in editor:

```
# type or copy paste your code here
print("Hello")
```

@gunungpw, @crypto_rayray

in if condition you don’t need to compere rv_type value “a”

character (lowercase character),

because you use .upper() method so rv_type always will be uppercase character

if rv_type in (“A”):
newStr = rv_class[0]
show_rv_type(newStr)

Just to follow up here, this doesn’t work the way you’re thinking.

Provided rv_type is a single character you get a correct result, and I
supposed multiple characters eg rv_type == "ABC" will also correctly
not match, but an empty rv_type will (incorrectly) match.

This is because the expression:

("A")

is not a 1-tuple containing 1 string. Instead, it is just the string
"A". This is because brackets do grouping, and (x) is equivalent to
just x. As in arithmetic:

x = (3 + 5) * 8  # 64, not 43
x = (3) * 8      # 24

The brackets in the second one are legal, but redundant, and just means
3. So this expression:

rv_type in "A"    # the same as your ("A")

is actually a substring search: does the string in rv_type occur
anywhere in the string "A"? If rv_type was not empty you would get a
correct test result (just by chance, because "A" only has one
character in it), but if rv_type were the empty string, the test would
always be true, because every string contains the empty string. So the
test is not actually the correct way to write this.

To make a tuple, Python uses some syntaxic forms which would not
normally occur with plain “brackets for grouping”:

()      # empty tuple
(x,)    # 1-tuple containing x
(x,y)   # 2-tuple containing x and y

so you need s special shape for the 0-tuple and 1-tuple forms. Which
means your if-statement should look like:

if rv_type in ("A",):

Of course, since you’re comparing to 1 string, you can also write:

if rv_type == "A":

Finally, a more normal way of associating one value with another is to
use a dict (more generally, a “mapping”):

replace_mapping = {
    'A': 'Class A',
    'B': 'Class B',
    ... etc etc ...
}

then something like one of these:

if rv_type in replace_mapping:
    newStr = replace_mapping[rv_type]
else:
    ... not recognised, do something else ...

or:

try:
    newStr = replace_mapping[rv_type]
except KeyError:
    ... not recognised, do something else ...

or:

newStr = replace_mapping.get(rv_type)
if newStr is None:
    ... not recognised, do something else ...

Which you might choose depends on your personal style and also on what
your larger code is doing.

Cheers,
Cameron Simpson cs@cskk.id.au

Thank you @cameron :slight_smile: