The Student Room Group

computer science python coding

How would I repeat a block of code that is the same depending on if thr user wants to purchase an additional device. My code is this:
print(*mobile_device,sep='\n')
device_choice = input('Choose phone or tablet from the list above(1 to 10) ')
while True:
if device_choice == 1:
total_price = total_price + Price[0]
elif device_choice == 2:
total_price = total_price + Price[1]
elif device_choice == 3:
total_price = total_price + Price[2]
else:
print(*mobile_device,sep='\n')
device_choice = input('Choose phone or tablet from the list above(1 to 10) ')
while True:
if charger_choice == 1:
print('you do not want to buy a charger')
elif charger_choice == 2:
total_price = total_price + price[13]
items_purchased[3] = charger_choice[0]
elif charger_choice == 3:
total_price = total_price + Price[14]
items_purchased[3] = charger_choice[1]
elif charger_choice == 4:
total_price = total_price + Price[14] + Price[15]
items_purchased[3] = charger_choice[0], charger_choice[1]
else:
charger_choice = input('Choose the charger(s) you want: 1) None or 2) Car or 3) Home or 4) Car and Home ').lowe
Original post by Yash12345
How would I repeat a block of code that is the same depending on if thr user wants to purchase an additional device. My code is this:


print(*mobile_device,sep='\n')
device_choice = input('Choose phone or tablet from the list above(1 to 10) ')
while True:
if device_choice == 1:
total_price = total_price + Price[0]
elif device_choice == 2:
total_price = total_price + Price[1]
elif device_choice == 3:
total_price = total_price + Price[2]
else:
print(*mobile_device,sep='\n')
device_choice = input('Choose phone or tablet from the list above(1 to 10) ')
while True:
if charger_choice == 1:
print('you do not want to buy a charger')
elif charger_choice == 2:
total_price = total_price + price[13]
items_purchased[3] = charger_choice[0]
elif charger_choice == 3:
total_price = total_price + Price[14]
items_purchased[3] = charger_choice[1]
elif charger_choice == 4:
total_price = total_price + Price[14] + Price[15]
items_purchased[3] = charger_choice[0], charger_choice[1]
else:
charger_choice = input('Choose the charger(s) you want: 1) None or 2) Car or 3) Home or 4) Car and Home ').lowe



It looks like you've already got a couple of while loops in there (Your indenting has probably been messed up by TSR's forum software though, so it's hard to tell exactly what your code is doing as a result of that)
-- However, you've written them as infinite/endless loops by using while True: which means you're not checking any kind of condition to cause those loops to end.


Remember that the while keyword will evaluate something for True/False before each cycle (just like an if check -- A key difference between if and while is that if happens once.) -- so consider the logic you'd need.
i.e. How would your program know whether the user wants to repeat again? are you going to ask them? If so, what value would they enter for yes or no?
(edited 4 years ago)
Reply 2
Original post by winterscoming
It looks like you've already got a couple of while loops in there (Your indenting has probably been messed up by TSR's forum software though, so it's hard to tell exactly what your code is doing as a result of that)
-- However, you've written them as infinite/endless loops by using while True: which means you're not checking any kind of condition to cause those loops to end.


Remember that the while keyword will evaluate something for True/False before each cycle (just like an if check -- A key difference between if and while is that if happens once.) -- so consider the logic you'd need.
i.e. How would your program know whether the user wants to repeat again? are you going to ask them? If so, what value would they enter for yes or no?

i would create a variable asking the user if they want to purchase an additional device. It would be (y/n) so the user would input y or n
Reply 3
Original post by Yash12345
i would create a variable asking the user if they want to purchase an additional device. It would be (y/n) so the user would input y or n

I thought that while True was validation
Original post by Yash12345
i would create a variable asking the user if they want to purchase an additional device. It would be (y/n) so the user would input y or n



Original post by Yash12345
I thought that while True was validation


while is a keyword which means:
# There may be some instructions here before the loop starts...

START:
IF (some condition is True) THEN
DO (one or more instructions)
JUMP to START
END IF

# There may also be some instructions here after the loop ends too.

In other words, it'll just keep on repeating until that condition ends up evaluating back to False -- so if you have an instruction inside that loop which is reading user input, then you could include that user input as part of the check .
- for example "WHILE (the user wants to buy a device)".
You could use the variable storing the 'y' or 'n' value in that condition.

(You might find it useful to start out by giving the variable an initial value of 'y' just to get the loop rolling for the first time).
(edited 4 years ago)

Quick Reply

Latest

Trending

Trending