【问题标题】:While and Break statements for python 2.7python 2.7的while和break语句
【发布时间】:2017-03-26 00:43:07
【问题描述】:

我对 python 比较陌生,所以我想知道是否有人可以帮助我找出我的 while 循环和 break 语句。

我可以让它继续或中断,但不能两者兼而有之。

我对该主题进行了研究,发现我发现的所有示例都倾向于数字等,或者提供的信息对我来说并不适用。

因此,我们将不胜感激。

该计划旨在计算您的每周工资。

当真时:

Hours_Worked = int(raw_input("How many hours have you worked this week?:\n"))#This line of code allows the user to input the amount of hours they have worked in whole numbers only.


Hourly_Rate = float(raw_input("What is your hourly rate?:\n" u"\u00A3"))#This line of code allows the user to input their hourly rate of pay in floating numbers to wither a whole or decimal points.


Tax = int(raw_input("What is your current Tax rate?:\n" u"\u0025"))#This line of code allows the user to input their tax rate.


Nat = int(raw_input("What is your National Insurance rate?:\n" u"\u0025"))#This line of code allows the user to input their National Insurance rate.


Total_Weekly_Pay = (Hours_Worked*Hourly_Rate)#This line of code works out the weekly pay before deductions.


Total_Weekly_Pay_After_Deductions = Total_Weekly_Pay - (Tax*Total_Weekly_Pay/100) - (Nat*Total_Weekly_Pay/100)# This line of code works out the weekly pay after deductions. The Tax and National Insurance rate are worked out by multiplying and then dividing.


print "Your Total weekly pay before deductions is\n" u"\u00A3%5.2f" %(Total_Weekly_Pay)#This line of code prints out the weekly pay before deductions to the floating points of 5.2.


print "Your Total weekly pay after Tax and National Insurance deductions is\n" u"\u00A3%5.2f" %(Total_Weekly_Pay_After_Deductions)#This line of code prints out the weekly pay after deductions to the floating points of 5.2.

cont = raw_input("Would you like to try again? (yes/no)\n")
if cont == "no":
    print "Goodbye"
    break

【问题讨论】:

  • raw_input 返回一个字符串,而不是 True/False
  • 您的空打印报表又是怎么回事?
  • 每当我在输入行的末尾添加“\n”时,它都会停止工作,所以我选择了空白打印,以使其暂时不那么混乱。

标签: python if-statement while-loop break


【解决方案1】:

打破如果应该是:

if cont == "":

或者你告诉用户退出的值,所以:

cont = raw_input("Would you like to try again?  (Yes/No")
if cont == "No":

【讨论】:

  • 可能想解释一下 raw_input 返回什么以及为什么比较不同的对象不起作用
  • 好的 iv 尝试了你的建议并设法让它发挥作用,还解决了空的打印选项等等。
  • @MooingRawr 在编辑中做到了这一点,但失去了连接:S Darren,基本上 raw_input 返回一个字符串,即使为空仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
  • 2021-05-07
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
相关资源
最近更新 更多