1
rent_expense_choose = str(input("Do you want a Custom rent expense integer or the Industry 
Standard, please enter C for custom and IS for industry standard: "))
if rent_expense_choose == "C":
    rent_expense = float(input("Please input the percentage of the rent which will be 
dedicated to expenses, awnser as a decimal e.g 0.24 = 24%: "))
elif rent_expense_choose == "IS":
    rent_expense = int(0.24)
else: print("please input either C or IS")

I want to turn this code into a while loop and I will explain how the loop will work. So the code asks for an input either C or IS, if the input is C the user will be prompted with an input and be asked another question but if the input is IS rent_expense will be assigned the value 0.24. What I wanted to do is create a while loop which when the user input is not either C or IS it loops the question again until the user inputs either C or IS so the code can move forward. Would appreciate some help and if the question seems stupid please don't flame me because i'm new.

Commoq
  • 15
  • 6
  • You could wrap the entire code fragment in *while True:*. Note that *int(0.24) == 0*. Note also that converting the return value of *input()* to *str* is pointless as it's already a string type. Elsewhere you convert the return from *input()* to *float* without allowing for bad input. This could cause an unwanted Exception – DarkKnight Jan 19 '22 at 09:02

2 Answers2

0
rent_expense_choose = ""
while rent_expense_choose not in ["C", "IS"]:
    rent_expense_choose = str(input(">"))

And the rest of your code with ifs.

matszwecja
  • 6,357
  • 2
  • 10
  • 17
0

This code should work, if you want to do it with a while loop

rent_expense_choose = ""
while rent_expense_choose not in ("IS", "C"):
    print("please input either C or IS")
    rent_expense_choose = input("Do you want a Custom rent expense integer or the Industry "
                                "Standard, please enter C for custom and IS for industry standard: ").upper()

if rent_expense_choose == "C":
    rent_expense = float(input("Please input the percentage of the rent which will be "
                               "dedicated to expenses, awnser as a decimal e.g 0.24 = 24%: "))
elif rent_expense_choose == "IS":
    rent_expense = 0.24

And if you don't trust the user to write an exact input in the first instance, you probably want to have some error handling when getting the float too.

Your rent_expense_choose == "IS": were also converted to an int, meaning 0. You don't have to specify data type, you can just write rent_expense = 0.24 if yo uwant it as a float.

  • Why *rent_expense = int(0.24)*? *rent_expense = 0* is equivalent – DarkKnight Jan 19 '22 at 09:04
  • Was just copied from original question, was just about to make an edit about that... – Another Noob Jan 19 '22 at 09:07
  • Appreciate the help, I actually understand while loops now based on the format you showed me. Lots of people don't respect beginner level programmers i'm not sure why, I get flamed all the time for being "Bad". Appreciate the help. – Commoq Jan 19 '22 at 18:42