How can I go back to a line in my code if a condition is not met | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)

6th Nov 2022, 12:44 PM
Gregory Maduagwu
Gregory Maduagwu - avatar
12 Answers
+ 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':`.
6th Nov 2022, 1:10 PM
Lothar
Lothar - avatar
+ 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')
6th Nov 2022, 4:33 PM
Lothar
Lothar - avatar
+ 3
Sorry for late response: I'm having some issues with sololearn https://code.sololearn.com/cdDlV4ibb1Ae/?ref=app
6th Nov 2022, 3:00 PM
I am offline
I am offline - avatar
+ 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.
6th Nov 2022, 5:18 PM
Sandeep
Sandeep - avatar
+ 1
replace if with while
6th Nov 2022, 12:49 PM
I am offline
I am offline - avatar
+ 1
Lothar using the OR operator will allow intake of anything else inculding the already defined data's
6th Nov 2022, 1:12 PM
Gregory Maduagwu
Gregory Maduagwu - avatar
+ 1
Lothar can we connect on WhatsApp?
6th Nov 2022, 4:56 PM
Gregory Maduagwu
Gregory Maduagwu - avatar
+ 1
Sandeep thanks for the advice sir
10th Nov 2022, 9:00 AM
Gregory Maduagwu
Gregory Maduagwu - avatar
0
Can you do it so i can see
6th Nov 2022, 12:50 PM
Gregory Maduagwu
Gregory Maduagwu - avatar
0
It worked out sir, you really deserve the pro badge
6th Nov 2022, 4:56 PM
Gregory Maduagwu
Gregory Maduagwu - avatar
0
I am new in progrmming ned help for my first program
8th Nov 2022, 10:56 AM
Dramera Fatoumata
- 1
Lothar here's my contact +2349064394824 hit me up sir
6th Nov 2022, 4:57 PM
Gregory Maduagwu
Gregory Maduagwu - avatar