python无限循环语句的代码_Python:我陷入了无限循环

1586010002-jmsa.png

The problem states as follows:

Example output:

Calculator

Give the first number: 100

Give the second number: 25

(1) +

(2) -

(3) *

(4) /

(5)Change numbers

(6)Quit

Current numbers: 100 25

Please select something (1-6): 5

Give the first number: 10

Give the second number: 30

(1) +

(2) -

(3) *

(4) /

(5)Change numbers

(6)Quit

Current numbers: 10 30

Please select something (1-6): 1

The result is: 40

(1) +

(2) -

(3) *

(4) /

(5)Change numbers

(6)Quit

Current numbers: 10 30

Please select something (1-6): 6

Thank you!

And my solution was:

print("Calculator")

var1 = input("Give the first number: ")

var2 = input("Give the second number: ")

print("(1) +\n(2) -\n(3) *\n(4) /\n(5)Change numbers\n(6)Quit")

print("Current numbers: " + var1 + " " + var2)

opcion = input("Please select something (1-6): ")

while (opcion != "6"):

if (opcion == "1"):

var3 = int(var1) + int(var2)

print("The result is: " + str(var3))

elif (opcion == "2"):

var3 = int(var1) - int(var2)

print("The result is: " + str(var3))

elif (opcion == "3"):

var3 = int(var1) * int(var2)

print("The result is: " + str(var3))

elif (opcion == "4"):

var3 = int(var1) / int(var2)

print("The result is: " + str(var3))

elif (opcion == "5"):

var1 = input("Give the first number: ")

var2 = input("Give the second number: ")

opcion = input("Please select something (1-6): ")

else:

print("Selection was not correct.")

print("Thank you!")

But in "elif (opcion == "5"):" the automatic corrector states that I fell in an infinite loop, I tried a break after the two new numbers are entered, but it completely exits from the while-loop, any ideas? Thank you.

解决方案

Problem in your code:

You only assign opcion outside the while loop and after which you are only modifying opcion when opcion == "5".

What happens when you give opcion = "1" is it adds and gives you the answer and after which checks the while statement when is true, because opcion did not change. It then adds again, and checks again, and goes oooooooooooon, so an infinite loop is produced.

Modification:

print("Calculator")

var1 = input("Give the first number: ")

var2 = input("Give the second number: ")

print("(1) +\n(2) -\n(3) *\n(4) /\n(5)Change numbers\n(6)Quit")

print("Current numbers: " + var1 + " " + var2)

while True:

opcion = input("Please select something (1-6): ")

if (opcion == "1"):

var3 = int(var1) + int(var2)

print("The result is: " + str(var3))

elif (opcion == "2"):

var3 = int(var1) - int(var2)

print("The result is: " + str(var3))

elif (opcion == "3"):

var3 = int(var1) * int(var2)

print("The result is: " + str(var3))

elif (opcion == "4"):

var3 = int(var1) / int(var2)

print("The result is: " + str(var3))

elif (opcion == "5"):

var1 = input("Give the first number: ")

var2 = input("Give the second number: ")

elif (opcion == "6"):

break

else:

print("Selection was not correct.")

print("Thank you!")

More improved solution:

import operator

print("Calculator")

var1 = input("Give the first number: ")

var2 = input("Give the second number: ")

print("(1) +\n(2) -\n(3) *\n(4) /\n(5)Change numbers\n(6)Quit")

print("Current numbers: " + var1 + " " + var2)

operations={"1":operator.add,"2":operator.sub,"3":operator.mul,"4":operator.div}

while True:

option = input("Please select something (1-6): ")

print option

if option in operations:

var3=operations[option](int(var1),int(var2))

print "value"+str( var3)

continue

if (option == "6"):

print "quiting"

break

elif (option == "5"):

var1 = input("Give the first number: ")

var2 = input("Give the second number: ")

else:

print("Selection was not correct.")

print("Thank you!")

Note :

It should be options and not opcion in correct English terms but it really doesn't matter :P

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值