0

I have a problem with my code and I think it's because of my while loop. Can someone help me with it? Sorry if comments are complicated to understand (I added comments in my native language first).

Edit 1: I forget to change taxes variable to kambiyo_yuzde, so it should have been look like this:

The problem is, the while loop doesn't end at value i want to get. For example, when i entered "usd_bolu_tl" "7.500" and "sahip_tl" "2000", value of satis_tl should be 266,67.000 which is 1866.2, and value of deger_tl should be 2004. What i want to calculate with my while loop is that it should add 0.001 value to the "yeni_usd" variable every time it loops and end when satis_tl's value (which is 266.67.000+0.001) is bigger than deger_tl (which is 2004)

kambiyo_yuzde = 2 / 1000
usd_bolu_tl = input("current rate: ")

sahip_tl = input("how much money you have in turkish liras: ")
# money plus taxes
deger_tl = float(sahip_tl) + (float(sahip_tl) * float(kambiyo_yuzde))

print("Bu tl değerinde dolar alabilmek için kambiyo vergisi ile beraber ", deger_tl, "tl ödemeniz gerekir.")

# usd we bought with turkish liras
alinacak_usd = float(sahip_tl) / float(usd_bolu_tl)

# new currency
yeni_usd = 7.000
# selling
satis_tl = float(alinacak_usd) * float(yeni_usd)

while satis_tl < deger_tl:
    print("yeni dolarin degeri: ", yeni_usd)
    yeni_usd += 0.001

# to profit
print("minimum kar icin dolarinizi satmaniz gereken deger: ", yeni_usd, "'dir.")

input("cikis yapmak icin herhangi bir tusa basiniz.")
5
  • 2
    What is the issue? Commented Dec 31, 2020 at 11:21
  • 1
    You are using kambiyo_yuzde before assigning any value to it Commented Dec 31, 2020 at 11:21
  • 1
    Since you have a condition already for the while loop, I bet you don't need that break statement, even because it would be inside some if-statement body if that was correct ;) Commented Dec 31, 2020 at 11:22
  • 1
    your loop will run exactly once (if condition is true to begin with) due to break. If that is a choice, why use while at all? (Also the loop has no update statement; an update statement would modifiy either satis_tl or deger_tl within the loop). Commented Dec 31, 2020 at 11:24
  • 1
    You. need to recalculate satis_tl = float(alinacak_usd) * float(yeni_usd) inside the loop if you want the loop to break naturally. Also remove break from the while loop Commented Dec 31, 2020 at 11:36

1 Answer 1

1

The break statement inside the while loop will terminate the execution of the loop after the first iteration, so you'd need to reconsider to remove that break at all or reconsider the initial condition to stop your while loop.

Additionally, as per the section of your code shared I don't see that you have initialized any values for the kambiyo_yuzde variable.

Sign up to request clarification or add additional context in comments.

4 Comments

usd_bolu_tl = input("current rate: ") initializes the variable!
Good catch! I missed that part! Let me edit the answer.
The thing is OP hasn't mentioned his problem!
Thank you, i removed break code and edited my question to explain my question better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.