0
How can I go back to a line in my code if a condition is not met
Card_type = input("Do you use an Amex or Visa card? ") if Card_type == 'Visa' and 'Amex': print('accepted') else: #here i need a code that will take me back to line 1 Print(line 1)
12 Réponses
+ 11
Austin Gregory ,
as Snehil Pandey already mentioned, we need a loop that can repeate a part of the code if a condition is not met or not. the input() function needs to be inside the loop.
if the condition is met, we can exit the loop by using `break`.
[EDITED]:
> please also check the conditional statement in: `if Card_type == 'Visa' and 'Amex':`.
+ 8
Austin Gregory ,
sorry, i was wrong with my suggestion to use `or' operator. i did not make sufficient test cases.
anyway. i have made a a test code for you, using the `and` operator.
> input: `Visa` -> shows ok
> input: `Amex` -> shows not ok
so the best way would be to use the in operator:
while True:
Card_type = input("Do you use an Amex or Visa card? ")
#if Card_type == 'Visa' and 'Amex':
if Card_type in ['Visa', 'Amex']:
print('accepted')
break
else:
#here i need a code that will take me back to line 1
print('wrong input')
+ 3
Sorry for late response:
I'm having some issues with sololearn
https://code.sololearn.com/cdDlV4ibb1Ae/?ref=app
+ 3
Hi Austin Gregory :)
Q/A is like a public forum. Anyone can see your number. Therefore, its not a good idea to post your number publicly.
To prevent yourself from any possible spam, you should remove your number.
+ 1
replace if with while
+ 1
Lothar using the OR operator will allow intake of anything else inculding the already defined data's
+ 1
Lothar can we connect on WhatsApp?
+ 1
Sandeep thanks for the advice sir
0
Can you do it so i can see
0
It worked out sir, you really deserve the pro badge
0
I am new in progrmming ned help for my first program
- 1
Lothar here's my contact +2349064394824 hit me up sir