I've noticed that when I remove a item, I need to select the checkout option multiple times.
Anybody know why this is happening?
my_cart = []
def customerDecision(addItem, input1):
while True:
customerResponse = input("What would you like to do? add / remove / show / checkout / quit ")
if customerResponse.lower() == "add":
addItem = input('add what? ')
my_cart.append(addItem)
elif customerResponse.lower() == "show":
print(my_cart)
elif customerResponse.lower() == "remove":
input1 = input(f'remove what: {my_cart} ')
print(f'{input1} removed...')
elif input1 in my_cart:
my_cart.remove(input1)
elif customerResponse.lower() == "checkout":
for addItem in my_cart:
print(f"Thanks for purchasing {my_cart}: '\n'see you next time!")
break
elif customerResponse.lower() == "quit":
print("Thank you! We hope you find what you're looking for next time")
break
else:
print("Please try a valid command.")
customerDecision(addItem='', input1='')
Like this:
elif customerResponse.lower() == "remove":
input1 = input(f'remove what: {my_cart} ')
if input1 in my_cart:
my_cart.remove(input1)
print(f'{input1} removed...')
By the way, it would be more efficient if you said
customerResponse = customerResponse.lower()
once at the top, rather than calling that function 5 times.
Related
i have this problem:
def thing():
e = input("Are you sure you want to enter this menu?")
if e == "yes":
while True:
placeholder()
elif e == "no":
return menu() #<-- THIS
else:
print("invalid response")
def menu():
print("""
what do you want to do?
1. Enter thing
2. Don't enter thing
""")
ans = input("Enter your response here:\n")
if ans == "1":
thing()
elif ans == "2":
pass
else:
print("invalid response")
If e == no then it won't go to menu() like i expected but it just stopped and i need a way to make what happens after they input "no" to be menu() yet it will not do that. any way to fix?
i'm new on using Python Script, i've been using Jupyter Notebook and finally realized the differences between the two of them. In this code i'm trying to create a simple command using def, where i need to type the number 1 - 3.
def Twitter():
while True:
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
break
elif user_input == '2':
print('You picked Verification')
break
elif user_input == '3':
print('Return')
Opening()
break
else:
print('Type a number 1-3')
continue
user_input = ''
def Opening():
while True:
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
Twitter()
break
elif user_input == '2':
print('Instagram!')
break
else:
print('Type a number 1-2')
continue
I'm still very immature and I need help on when I picked the number 3, the function would return to the previous part instead of not outputing the result at all thanks!
Instead of using a break statement, you can return the function inside a function.
See: https://www.geeksforgeeks.org/returning-a-function-from-a-function-python/
def Twitter():
while True:
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
break
elif user_input == '2':
print('You picked Verification')
break
elif user_input == '3':
print('Return')
return Opening() # switch to Opening()
else:
print('Type a number 1-3')
continue
def Opening():
while True:
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
return Twitter() # switch to Twitter()
elif user_input == '2':
print('Instagram!')
break
else:
print('Type a number 1-2')
continue
if __name__ == '__main__':
Opening()
it works fine for me...
maybe you can try to call the function recursively instead of using while true?
def Twitter():
user_input = input(
'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')
if user_input == '1':
print('You picked Crawling')
Twitter()
elif user_input == '2':
print('You picked Verification')
Twitter()
elif user_input == '3':
print('Return')
Opening()
else:
print('Type a number 1-3')
Twitter()
user_input = ''
def Opening():
user_input = input(
'Pick one: 1) Twitter | 2) Instagram ---->')
if user_input == '1':
Twitter()
elif user_input == '2':
print('Instagram!')
Opening()
else:
print('Type a number 1-2')
Opening()
Twitter()
So I'm trying to figure out how I can make this simple little program to go back to the raw_input if the user inputs something else then "yes" or "no".
a = raw_input("test: ")
while True:
if a == "yes":
print("yeyeye")
break
elif a == "no":
print("nonono")
break
else:
print("yes or no idiot")
This is what I got so far, I'm new and it's hard to understand. Thanks in advance.
As #DavidG mentioned, just add your raw_input statement in loop:
while True:
a = raw_input("Enter: ")
if a == "yes":
print("You have entered Yes")
break
elif a == "no":
print("You have entered No")
break
else:
print("yes or no idiot")
Simply you can put the first instruction inside the loop; in this way, every time the user inserts a value different to yes or no you can print a message and wait to a new input.
while True:
a = raw_input("test: ")
if a == "yes":
print("yeyeye")
break
elif a == "no":
print("nonono")
break
else:
print("yes or no idiot")
Describe a condition checker for while and read input everytime when your condition is not meet. Inline returns are good for low quantity conditions but when your choice count is too much or condition in condition situations appear, inline returns are becoming trouble.
Thats why you must use condition checkers(like cloop) instead of inline returns.
cloop=True
while cloop:
a = raw_input("test: ")
if a == "yes":
print("yeyeye")
cloop=False
elif a == "no":
print("nonono")
cloop=False
else:
print("yes or no idiot")
cloop=True
I'm am writing a simple program that look something like this:
while True:
choice = float(input("options: "))
if choice == 1:
# Do something
elif choice == 2:
# Do something
elif choice == 3: # <--- seems redudant
while choice == 3:
choice_return = input("do you want to return?: ")
if choice_return == "yes":
choice = None
else:
pass
elif choice == 4:
break
As noted in the the code, the "elif statment" seems redundant because it has the same conditions as the "while loop" below. You can of course simply write the code as follows:
while True:
choice = float(input("options: "))
if choice == 1:
# Do something
elif choice == 2:
# Do something
elif choice == 4:
break
while choice == 3: <--- three after four, no!!!
choice_return = input("do you want to return?: ")
if choice_return == "yes":
choice = None
else:
pass
which don't look to bad in this example, but in the actual code, it kinda ruins the structuring (and my OCD don't allow for that). Is there a way I can remove the redundancy while maintaining order?
NB. assume the "choice number" is fixed.
You can stick to the if/elif structure and clean up things by wrapping the logic of elif choice == 3 in a function and using a while True recipe in the function:
def myfunc()
while True:
choice_return = input("do you want to return?: ")
if choice_return == "yes":
return None # None here is redundant
while True:
choice = float(input("options: "))
if choice == 1:
...
elif choice == 3:
choice = myfunc()
...
There doesn't seem to be any reason to use choice as the condition in the while loop. Restructure it so it uses break to stop the loop when appropriate.
elif choice == 3: # <--- seems redudant
while True:
choice_return = input("do you want to return?: ")
if choice_return == "yes":
break
Put the inner while loop into a function, the code will then be much cleaner. You will still have both tests, but integer comparisons are fast - so why are you using float?
Better yet, if you could but every "case" into a function you could use a list of functions.
# Functions return True to break out of the loop, otherwise False
def case1():
# Do something
def case2():
# Do something
def case3():
# Do something
def case4():
# Do something
cases = [case1, case2, case3, case4]
while True:
choice = int(input("options: "))
if choice < len(cases) and choice >= 0:
if cases[choice]():
break
else:
print("Invalid option", choice)
Newbie python here. How can I break out of the second while loop if a user selects "Q" for "Quit?"
If I hit "m," it goes to the main menu and there I can quit hitting the "Q" key.
while loop == 1:
choice = main_menu()
if choice == "1":
os.system("clear")
while loop == 1:
choice = app_menu()
if choice == "1":
source = '%s/%s/external' % (app_help_path,app_version_10)
target = '%s/%s' % (target_app_help_path,app_version_10)
elif choice == "2":
source = '%s/%s/external' % (app_help_path,app_version_8)
target = '%s/%s' % (target_app_help_path,app_version_8)
elif choice.lower() == "m":
break
loop = 0
elif choice.lower() == "q":
break
loop = 0
sendfiles(source, target)
# Internal files
elif choice == "q":
loop = 0
App menu method:
def app_menu()
print "Select APP version"
print "-------------------"
print "1) 11"
print "2) 10"
print "3) 8"
print "m) Main Menu"
print "q) Quit"
print
return raw_input("Select an option: ")
You nearly have it; you just need to swap these two lines.
elif choice.lower() == "m":
break
loop = 0
elif choice.lower() == "m":
loop = 0
break
You break out of the nested loop before setting loop. :)
Change
break
loop = 0
to
loop = 0
break
in your elif blocks.
Use an exception.
class Quit( Exception ): pass
running= True
while running:
choice = main_menu()
if choice == "1":
os.system("clear")
try:
while True:
choice = app_menu()
if choice == "1":
elif choice == "2":
elif choice.lower() == "m":
break
# No statement after break is ever executed.
elif choice.lower() == "q":
raise Quit
sendfiles(source, target)
except Quit:
running= False
elif choice == "q":
running= False
Use two distinct variables for both loops, eg loop1 and loop2.
When you first press m in the inner loop you just break outside, and then you can handle q separately.
By the way you shouldn't need the inner variable to keep looping, just go with an infinite loop until key 'm' is pressed. Then you break out from inner loop while keeping first one.
Rename your top loop to something like mainloop, and set mainloop = 0 when q is received.
while mainloop == 1:
choice = main_menu()
if choice == "1":
os.system("clear")
while loop == 1:
choice = app_menu()
if choice == "1":
source = '%s/%s/external' % (app_help_path,app_version_10)
target = '%s/%s' % (target_app_help_path,app_version_10)
elif choice == "2":
source = '%s/%s/external' % (app_help_path,app_version_8)
target = '%s/%s' % (target_app_help_path,app_version_8)
elif choice.lower() == "m":
loop = 0
break
elif choice.lower() == "q":
mainloop = 0break
break
sendfiles(source, target)
# Internal files
elif choice == "q":
mainloop = 0
You could put this into a function and return:
import os.path
def do_whatever():
while True:
choice = main_menu()
if choice == "1":
os.system("clear")
while True:
choice = app_menu()
if choice in ("1", "2"):
app_version = app_version_10 if choice == "1" else app_version_8
source = os.path.join(app_help_path, app_version, "external")
target = os.path.join(target_app_help_path, app_version)
sendfiles(source, target)
elif choice.lower() == "m":
break
elif choice.lower() == "q":
return
Admittedly, I don't quite get when you want to break the inner loop and when you want to quit both loops, but this will give you the idea.