I want to get the inputs in a loop and I want to show the user they are entering data for the 1st,2nd,3rd, etc items in that loop, can anyone help me please with fixing my code?
n=int(input("Pleaseenter the number of laptops: "))
i=0
while i!=n:
laptops_price=input("Please enter the price of the {i}laptop: ".format(i))
i =1
CodePudding user response:
You have confused f
strings with format
calls. They are not quite the same. Either of these will work:
laptops_price=input(f"Please enter the price of the {i}laptop: ")
laptops_price=input("Please enter the price of the {}laptop: ".format(i))
CodePudding user response:
Use {}
not {i}
, and use a for i in range(n)
loop instead - its much more convenient
Also, Im not sure your exact purpose, but make laptops_prices
a list and use .append() instead
CodePudding user response:
Okay, So there are a couple things worth considering here:
Firstly, the question seems a little vague to me, in a sense you can just tab out your code to make it work properly, with some minor changes.
Second, you may want an ordinal dict setup to change name of laptop from '1' to '1st' etc, but this is fairly trivial.
Third, there is the matter of the loop you are using. Preferably it would use a range.
So,
Adjusted Version (working):
n=int(input("Please enter the number of laptops: "))
i=0
while i!=n:
laptops_price=input("Please enter the price of laptop " str(i) ": ".format(i))
i =1
While the above works, it might benefit from some changes. New Version (working):
n=int(input("Please enter the number of laptops: "))
for i in range(n):
laptops_price=input("Please enter the price of laptop " str(i 1) ": ".format(i))
and if you wanted a dictionary for 1st, 2nd, 3rd, 4th, etc. Use this instead:
n=int(input("Please enter the number of laptops: "))
for i in range(n):
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n//10!=1)*(n<4)*n::4])
laptops_price=input("Please enter the price the " ordinal(i 1) " laptop: ".format(i))