Hello. I’m new to Python (and coding in general). I’m trying to make a sample store, and I want to ask the user if they have a coupon. If they have a coupon they have to type in the coupon code to get a discount. Everything works up until the part where the wrong coupon code is entered and it skips my loop entirely. I want it to ask for the coupon code again, hence why it’s in a while loop. What am I doing wrong? Thanks
*Here’s what I have:
coupon_code = "EAT"
coupon = ()
coupon = input("Do you have a coupon? (Y/N): ")
if coupon.lower() == "y":
coupon = bool(coupon)
coupon = True
elif coupon.lower() == "n":
coupon = bool(coupon)
coupon = False
print("Too bad LOL")
time.sleep(1)
else:
coupon = bool(coupon)
coupon = False
while coupon != coupon_code and coupon is True:
coupon = input("Please enter your coupon code (press E to escape): ")
if coupon == coupon_code:
coupon = bool(coupon)
coupon = True
print("Success! Coupon applied.")
time.sleep(1)
break
elif coupon == "E":
print("Yeah that's what I though! You ain't got no coupon.")
coupon = bool
coupon = False
time.sleep(1)
break
else:
print(f"{coupon} was a nice try, but WRONG!!")
You don’t import time, though I suspect that you didn’t copy the code exactly as you’ve been running it (there’s a stray < there too).
You overwrite the boolean coupon variable with the string that the user entered. Non-empty strings are truthy (evaluate to True if you call bool with the string, which is effectively what if ... does.
You probably want something like the following:
import time
coupon_code = "EAT"
has_coupon = None
while has_coupon is None:
coupon = input("Do you have a coupon? (Y/N): ")
if coupon.lower() == "y":
has_coupon = True
elif coupon.lower() == "n":
has_coupon = False
print("Too bad LOL")
time.sleep(1)
if has_coupon:
while True:
trial_coupon = input("Please enter your coupon code (press E to escape): ")
if trial_coupon == coupon_code:
print("Success! Coupon applied.")
time.sleep(1)
break
elif trial_coupon == "E":
print("Yeah that's what I though! You ain't got no coupon.")
time.sleep(1)
break
else:
print(f"{trial_coupon} was a nice try, but WRONG!!")
Haha thanks, the < was an accident typed into the forum (I removed it).
I have more code, I just wanted to shorten the post by only putting what’s relevant. Basically, the issue is that when I type anything other than the coupon code or the letter E, it breaks out of the while loop instead of asking for the coupon code again. I can’t figure this out
I had to change a few things, so it looks like this now
coupon_code = "EAT"
has_coupon = None
coupon = ()
while has_coupon is None:
coupon = input("Do you have a coupon? (Y/N): ")
if coupon.lower() == "y":
has_coupon = True
elif coupon.lower() == "n":
has_coupon = False
print("Too bad LOL")
time.sleep(1)
if has_coupon:
while True:
coupon = input("Please enter your coupon code (press E to escape): ")
if coupon == coupon_code:
print("Success! Coupon applied.")
time.sleep(1)
coupon = True
break
elif coupon == "E":
print("Yeah that's what I thought! You ain't go no coupon.")
time.sleep(1)
has_coupon = False
break
else:
print(f"{coupon} was a nice try, but WRONG!!")
Your initial assignment of coupon is to an empty tuple (). If you want a “default” value that indicates “not set”, then you should often use None, i.e. coupon = None.
You never need to read coupon again, so don’t worry about assigning it to None, i.e. just remove coupon = () / coupon = None from line three.
In general, don’t re-use variables for different purposes, especially not for different types. I’d recommend not setting coupon = True inside your success case (Success! Coupon applied.). Instead create a new variable if you need.