【问题标题】:Return command within Python [closed]Python中的返回命令[关闭]
【发布时间】:2017-08-01 03:49:48
【问题描述】:

问题

我刚开始接触python。我正在尝试将我正在处理的这个基本程序循环到开始。

当前进程只是中断程序,我不确定我错过了什么。我怎样才能重新调整返回/选择的方向来解决这个问题?

项目信息

  • 要达到的最终公式类似于 (直径 * 数量 = x1) ~ x1 对于正确的导管尺寸表来说是真或假。

  • 直径将取决于电缆类型 - 稍后将添加多种状态,以便混合和匹配类型/数量以及添加电缆类型以方便使用。

程序开始

import random
import sys
import os

def prog01():
    print("")
od = float(input("Please input the outer dimension of your wire size in decimal form: "))
quantity = float(input("Please choose how many cables you have: "))

# diameter of cabling
def outer(od):
    try:
        od = float(od)
        print (od * quantity)
    except ValueError:
        print ('A numeric value was not input in the program. Please only use numeric information')

# quantity of cabling
def number(quantity):
    try:
        quantity = float(quantity)
    except ValueError:
        print ('A numeric value was not input in the program. Please only use numeric information')

# reference
outer(od)
number(quantity)


def select_again():

        while True:
            again = input("Do you have more cable types to add to your system? Please type y for yes or n for no: ")
            if again not in {"y","n"}:
                print("please enter valid input")
            elif again == "n":
                break
            elif again == "y":
                return prog01()


# sizing tables - true/false statements
x1 = (od * quantity)

# emt_list = over 2 wires @ 40% ['.122', '.213', '.346', '.598', '.814', '1.342', '2.343', '3.538', '4.618', '5.901']
emt_list = ['1/2" Conduit','3/4" Conduit','1" Conduit','1&1/4" Conduit', '1&1/2" Conduit','2" Conduit','2&1/2" Conduit',
            '3" Conduit','3&1/2" Conduit','4" Conduit',]

if x1 <= .122:
    print (emt_list [0])
elif x1 <= .213:
    print (emt_list [1])
elif x1 <= .346:
    print (emt_list [2])
elif x1 <= .598:
    print (emt_list [3])
elif x1 <= .814:
    print (emt_list [4])
elif x1 <= 1.342:
    print (emt_list [5])
elif x1 <= 2.343:
    print (emt_list [6])
elif x1 <= 3.538:
    print (emt_list [7])
elif x1 <= 4.618:
    print (emt_list [8])
elif x1 <= 5.901:
    print (emt_list [9])
if x1 >= 5.902:
    print ('You will need more than one piece of conduit')

select_again()

# rmc_list to come = over 2 wires @ 40% []

【问题讨论】:

  • 不知道你的问题是什么,请你澄清一下吗?

标签: python loops while-loop return


【解决方案1】:

在您当前的代码中发生的情况是,当您再次进入选择循环时,它只会在您输入“n”时离开输入循环本身。当你输入 'y' 时,函数 prog01() 会运行,但它所做的只是打印一个新行并返回 None,因此程序返回到 select again 循环。

要让它做你想做的事,你应该用一个 while True 循环语句替换函数 prog01(),并在 'y' 或 'n' 上为 yes 或 no 查询设置单独的 while True 循环, 如果输入返回为 'n',则外循环中断。

我还在输入查询的末尾添加了 .strip().lower() 以解决意外空格并删除区分大小写,并将其更改为 raw_input() 调用,因为我不知道是否您使用 python 2.x 或 3.x。如果您使用的是 3.0+,则可以将其保留为 input() 调用。

import random
import sys
import os

def outer(od):
    try:
        od = float(od)
        print (od * quantity)
    except ValueError:
        print ('A numeric value was not input in the program. Please only use numeric information')
def number(quantity):
    try:
        quantity = float(quantity)
    except ValueError:
        print ('A numeric value was not input in the program. Please only use numeric information')

while True:
    od = float(input("Please input the outer dimension of your wire size in decimal form: "))
    quantity = float(input("Please choose how many cables you have: "))

    # diameter of cabling


    # quantity of cabling

    # reference
    outer(od)
    number(quantity)

    # sizing tables - true/false statements
    x1 = (od * quantity)

    # emt_list = over 2 wires @ 40% ['.122', '.213', '.346', '.598', '.814', '1.342', '2.343', '3.538', '4.618', '5.901']
    emt_list = ['1/2" Conduit','3/4" Conduit','1" Conduit','1&1/4" Conduit', '1&1/2" Conduit','2" Conduit','2&1/2" Conduit',
                '3" Conduit','3&1/2" Conduit','4" Conduit',]

    if x1 <= .122:
        print (emt_list [0])
    elif x1 <= .213:
        print (emt_list [1])
    elif x1 <= .346:
        print (emt_list [2])
    elif x1 <= .598:
        print (emt_list [3])
    elif x1 <= .814:
        print (emt_list [4])
    elif x1 <= 1.342:
        print (emt_list [5])
    elif x1 <= 2.343:
        print (emt_list [6])
    elif x1 <= 3.538:
        print (emt_list [7])
    elif x1 <= 4.618:
        print (emt_list [8])
    elif x1 <= 5.901:
        print (emt_list [9])
    if x1 >= 5.902:
        print ('You will need more than one piece of conduit')

    again = raw_input("Do you have more cable types to add to your system? Please type y for yes or n for no: ").lower().strip()
    while True:
        if again not in {"y","n"}:
            print("please enter valid input")
        else:
            break
    if again == "n":
        break


    # rmc_list to come = over 2 wires @ 40% []

【讨论】:

  • 干杯,感谢您的帮助!
【解决方案2】:

如果你使用Python 2,你需要使用raw_input而不是input

【讨论】:

    猜你喜欢
    • 2015-04-19
    • 2013-09-30
    • 2020-04-07
    • 1970-01-01
    • 2014-01-13
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多