# Trying to replace input string with another string with multiple conditions

**URL:** <https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411>\
**Category:** Python Help\
**Tags:** help\
**Created:** [March 17, 2022, 11:06pm UTC](https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411 "2022-03-17T23:06:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![crypto\_rayray](https://avatars.discourse-cdn.com/v4/letter/c/58f4c7/32.png) [@crypto\_rayray](https://discuss.python.org/u/crypto_rayray)\
**Post date:** [March 17, 2022, 11:06pm UTC](https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411/1 "2022-03-17T23:06:02Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [March 18, 2022, 1:19am UTC](https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411/2 "2022-03-18T01:19:22Z")

</div>

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](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![crypto\_rayray](https://avatars.discourse-cdn.com/v4/letter/c/58f4c7/32.png) [@crypto\_rayray](https://discuss.python.org/u/crypto_rayray)\
**Post date:** [March 18, 2022, 6:14am UTC](https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411/3 "2022-03-18T06:14:20Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![gunungpw](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gunungpw/32/5832_2.png) [@gunungpw](https://discuss.python.org/u/gunungpw)\
**Post date:** [March 18, 2022, 7:59am UTC](https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411/4 "2022-03-18T07:59:13Z")

</div>

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

In your code:

```auto
# 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:

```auto
# 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;

```auto
# 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:

```auto
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:

```auto
# 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 👍

ref:

> **[Lists and Tuples in Python – Real Python](https://realpython.com/python-lists-tuples/#python-lists)**
>
> You'll cover the important characteristics of lists and tuples in Python 3. You'll learn how to define them and how to manipulate them. When you're finished, you should have a good feel for when and how to use these object types in a Python program.

---

<div class="post-metadata">

**Author:** ![gunungpw](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gunungpw/32/5832_2.png) [@gunungpw](https://discuss.python.org/u/gunungpw)\
**Post date:** [March 18, 2022, 8:08am UTC](https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411/5 "2022-03-18T08:08:30Z")

</div>

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

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

````

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [March 19, 2022, 9:57pm UTC](https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411/6 "2022-03-19T21:57:21Z")

</div>

@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](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![gunungpw](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gunungpw/32/5832_2.png) [@gunungpw](https://discuss.python.org/u/gunungpw)\
**Post date:** [March 20, 2022, 12:12am UTC](https://discuss.python.org/t/trying-to-replace-input-string-with-another-string-with-multiple-conditions/14411/7 "2022-03-20T00:12:08Z")

</div>

Thank you @cameron 🙂
