Python中的Return命令 问题:
我刚刚开始学习python。我试着把我正在做的这个基本程序循环到一开始 当前的过程只是中断程序,我不确定我遗漏了什么。如何重新定向返回/选择以实现此目标 程序信息Python中的Return命令 问题:,python,loops,while-loop,return,Python,Loops,While Loop,Return,我刚刚开始学习python。我试着把我正在做的这个基本程序循环到一开始 当前的过程只是中断程序,我不确定我遗漏了什么。如何重新定向返回/选择以实现此目标 程序信息 要达到的最终公式类似于(直径*数量=x1)~x1对于正确的导管尺寸表是真是假 直径将取决于电缆类型-稍后将添加多个状态,以便混合和匹配类型/数量,并添加电缆类型以方便使用 程序启动 随机导入 导入系统 导入操作系统 def prog01(): 打印(“”) od=浮动(输入(“请以十进制形式输入导线尺寸的外部尺寸:”) 数量=浮动
- 要达到的最终公式类似于(直径*数量=x1)~x1对于正确的导管尺寸表是真是假
- 直径将取决于电缆类型-稍后将添加多个状态,以便混合和匹配类型/数量,并添加电缆类型以方便使用
随机导入
导入系统
导入操作系统
def prog01():
打印(“”)
od=浮动(输入(“请以十进制形式输入导线尺寸的外部尺寸:”)
数量=浮动(输入(“请选择您有多少电缆:”)
#电缆直径
def外部(od):
尝试:
od=浮动(od)
打印(od*数量)
除值错误外:
print('程序中未输入数值。请仅使用数值信息')
#电缆数量
def编号(数量):
尝试:
数量=浮动(数量)
除值错误外:
print('程序中未输入数值。请仅使用数值信息')
#参考文献
外部(od)
数量
def再次选择_()
尽管如此:
再次=输入(“您有更多的电缆类型要添加到您的系统中吗?请键入y表示是,或键入n表示否:”)
如果再次不在{“y”,“n”}中:
打印(“请输入有效输入”)
elif再次==“n”:
打破
elif再次==“y”:
返回prog01()
#大小调整表-正确/错误陈述
x1=(外径*数量)
#emt_列表=超过2根电线@40%['.122'、'.213'、'.346'、'.598'、'.814'、'1.342'、'2.343'、'3.538'、'4.618'、'5.901']
emt_列表=['1/2“导管','3/4“导管','1“导管','1&1/4“导管','1&1/2“导管','2”导管','2&1/2“导管',
“3”导管“,“3&1/2”导管“,“4”导管“,]
如果x1如果您使用的是Python2
,则需要使用raw\u input
而不是input
如果您使用的是Python2
,则需要使用raw\u input
而不是input,当前代码中发生的情况是,当您再次输入select循环时,当您输入“n”时,它只离开输入循环本身。当您输入“y”时,函数prog01()将运行,但它所做的只是打印新行并返回None,因此程序将返回select REACH循环
为了让它执行您想要的操作,您应该用while True循环语句替换函数prog01(),并在“y”或“n”上为yes或no查询中断设置单独的while True循环,如果输入返回为“n”,则设置外部循环中断
我还将.strip().lower()添加到输入查询的末尾,以考虑意外空格并消除大小写敏感性,并将其更改为原始输入()调用,因为我不知道您使用的是python 2.x还是3.x。如果您使用的是3.0+,则可以将其保留为input()调用
随机导入
导入系统
导入操作系统
def外部(od):
尝试:
od=浮动(od)
打印(od*数量)
除值错误外:
print('程序中未输入数值。请仅使用数值信息')
def编号(数量):
尝试:
数量=浮动(数量)
除值错误外:
print('程序中未输入数值。请仅使用数值信息')
尽管如此:
od=浮动(输入(“请以十进制形式输入导线尺寸的外部尺寸:”)
数量=浮动(输入(“请选择您有多少电缆:”)
#电缆直径
#电缆数量
#参考文献
外部(od)
数量
#大小调整表-正确/错误陈述
x1=(外径*数量)
#emt_列表=超过2根电线@40%['.122'、'.213'、'.346'、'.598'、'.814'、'1.342'、'2.343'、'3.538'、'4.618'、'5.901']
emt_列表=['1/2“导管','3/4“导管','1“导管','1&1/4“导管','1&1/2“导管','2”导管','2&1/2“导管',
“3”导管“,“3&1/2”导管“,“4”导管“,]
如果x1当前代码中发生的情况是,当您输入select REACH循环时,它只在您输入'n'时离开输入循环本身。当您输入“y”时,函数prog01()将运行,但它所做的只是打印新行并返回None,因此程序将返回select REACH循环
为了让它执行您想要的操作,您应该用while True循环语句替换函数prog01(),并在“y”或“n”上为yes或no查询中断设置单独的while True循环,如果输入返回为“n”,则设置外部循环中断
我还将.strip().lower()添加到输入查询的末尾,以考虑意外空格并消除大小写敏感性,并将其更改为原始输入()调用,因为我不知道您使用的是python 2.x还是3.x。如果您使用的是3.0+,则可以将其保留为input()调用
随机导入
导入系统
导入操作系统
def外部(od):
尝试:
od=浮动(od)
打印(od*数量)
除值错误外:
print('程序中未输入数值。请仅使用数值信息')
def编号(数量):
尝试:
数量=浮动(数量)
除值错误外:
print('程序中未输入数值。请仅使用数值信息')
尽管如此:
od=浮动(输入(“请以十进制形式输入导线尺寸的外部尺寸:”)
数量=浮动(输入(“请选择您有多少电缆:”)
#电缆直径
#电缆数量
#参考文献
外部(od)
数量
#大小调整表-正确/错误陈述
x1=(外径*数量)
#emt_列表=超过2根电线@40%['.122'、'.213'、'.346'、'.598'、'.814'、'1.342'、'2.343'、'3.538'、'4.618'、'5.901'
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% []
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% []