Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python question plz help! thank you very much! how to use for loop to repeat an

ID: 3676042 • Letter: P

Question

python question plz help! thank you very much!

how to use for loop to repeat an program? for example,

yn= input("Do you want enter a new data?(Please answer this use yes or no):")
for yn=='yes':
name= input("Enter the name of the stock:")
num= int(input("Enter the stock number of shares joe bought:"))
p= float(input("Enter the stock purhase price:$"))
sp= float(input("Enter the stock selling price:$"))
c= float(input("Please enter the broker commission rate:"))
apaid=num *p
acpb=apaid*c
asell=num*sp
acps=asell*c
profit=asell-acpb-apaid-acps
print("The name of the sock is",name)
print("The amount of money you paid for the stock:$",format(apaid,'.2f'))
print("The amount of commission you paid your broker when you bought the stock:$",format(acpb,'.2f'))
print("The amount that you sold the stock:$",format(asell,'.2f'))
print("The amount of commission you paid your broker when you sold the stock:$",format(acps,'.2f'))
if profit>0:print("you earn $",format(profit,'.2f'))
elif profit==0:print("your profit is 0.")
else:print("you lost $",format(profit,'.2f'))
yn=input("Do you want enter a new data?(Please answer this use yes or no):")
else:print("thank you!")

i use while loop to repeat this program but how can i use for loop to repeat it? i would add a x=int(input("how many stocks do you want enter now?:")) an the beginning of this code.

Explanation / Answer

Use program as shown below :

OR Use the below program to achieve the same output as your program :

name = input("Enter the name of the stock or -9 to quit: ")


while name != "-9":
nos = int(input("Enter number of shares: "))
spp = float(input("Enter stock purchase price: "))
ssp = float(input("Enter stock selling price: "))
bc = float(input("Enter brokers commission: "))
pp = nos* spp
pbc = pp * bc
sp = nos * ssp
sbc = sp * bc
total_paid = pp + pbc
total_sold = sp - sbc
profit_loss = total_sold - total_paid
print("The name of the stock: ", name)
print("Amount paid for the stock: $", format(pp,',.2f'))
print("Commission paid for the purchase: $ ", format(pbc,',.2f'))
print("Amount the stock sold for: $", format(sp,',.2f'))
print("commission paid on the sale: $ ", format(sbc,',.2f'))

if profit_loss > 0:
print("You made a profit: $", format(profit_loss,',.2f'))
elif profit_loss == 0:
print("Nice risk, sell higher next time!",format(profit_loss,',.2f'))
else:
print("You lost money: $",format(profit_loss,',.2...
name = input("Enter the name of the stock or -9 to quit: ")

input()