Help! My Python Program Prints Incorrectly After the First Statement

0
21
Asked By CuriousCoder123 On

I'm trying to tackle a challenge on Sololearn where I need to write a program that prints 'accepted' if the input is either 'amex' or 'visa' and does absolutely nothing for any other input. Here's what I came up with so far:

```python
card_type = input()
if card_type == 'amex' or 'visa':
print('accepted')
else:
print('')
```

I attempted to modify it like this to check if the first part works:

```python
card = input()
if card == 'amex' or 'visa':
print('accepted')
```

But this returned 'accepted' for any input, not just the specific card types I'm looking for. I also tried this piece of code:

```python
card = input()
if card != 'amex' and card != 'visa':
print('')
```

which performed as expected, but I'm stuck with the first condition leading to unexpected outputs. I just started programming and have been trying multiple approaches without success, and it's frustrating since Sololearn wants a subscription for more help!

3 Answers

Answered By IndentationNinja On

Don't forget about indentation! Python relies heavily on whitespace to determine blocks of code. Make sure your `print` statements are properly indented to be part of the `if` or `else` statement. For example, this:
```python
if thing:
print('cow')
```
will behave differently from:
```python
if thing:
print('cow')
```
The first version is the correct one!

Answered By LogicLover On

You nailed it, but really think about what you're asking with your conditionals. `if card == 'amex' or 'visa'` is fundamentally different from `if card == 'amex' or card == 'visa'`. The second condition checks for both explicitly. To clarify, the string 'visa' always evaluates to true on its own, leading to misleading results. If you want tighter checks, make sure to use the proper boolean logic in your conditions!

CuriousCoder123 -

I did fix it! But is it common to feel confused all the time when first learning to code?

Answered By SyntaxSleuth90 On

It looks like there's a misunderstanding in your conditional statements. When you have `if card == 'amex' or 'visa':`, the check for 'visa' isn't connected to your variable `card`. The condition actually always evaluates as true because strings like 'visa' always return true if assessed as a boolean. Instead, you should write it like this: `if card == 'amex' or card == 'visa':`. This way, you're explicitly checking the value of `card` against both options!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.