Function Python如何定义一个只接受可用于多行的整数输入的函数
我正在尝试制作一个程序,让用户将数字输入到多行不同的代码中,我正在尝试这样做,如果用户输入的不是数字,程序将再次要求用户正确输入数字。我试图定义一个函数,我可以使用它们,但每次我运行程序,它崩溃。任何帮助都将不胜感激,谢谢 我的代码:Function Python如何定义一个只接受可用于多行的整数输入的函数,function,python-3.x,Function,Python 3.x,我正在尝试制作一个程序,让用户将数字输入到多行不同的代码中,我正在尝试这样做,如果用户输入的不是数字,程序将再次要求用户正确输入数字。我试图定义一个函数,我可以使用它们,但每次我运行程序,它崩溃。任何帮助都将不胜感激,谢谢 我的代码: def error(): global m1 global m2 global w1 global w2 while True: try: int(m1 or m2 or w1 or
def error():
global m1
global m2
global w1
global w2
while True:
try:
int(m1 or m2 or w1 or w2)
except ValueError:
try:
float(m1 or m2 or w1 or w2)
except ValueError:
m1 or m2 or w1 or w2=input("please input your response correctly...")
break
m1=input("\nWhat was your first marking period percentage?")
error()
w1=input("\nWhat is the weighting of the first marking period? (in decimal)")
error()
m2=input("\nWhat was your second marking period percentage?")
error()
w2=input("\nWhat is the weighting of the second marking period? (in decimal)")
error()
您应该编写函数以一次获取一个数字。如果在某个地方触发了at异常,则应该对其进行处理。请注意,下面显示的
get_number
函数将不断询问号码,但也会显示调用者指定的提示。如果您没有运行Python3.6或更高版本,则需要在main
函数中注释掉对print
的调用
#! /usr/bin/env python3
def main():
p1 = get_number('What is your 1st marking period percentage? ')
w1 = get_number('What is the weighting of the 1st marking period? ')
p2 = get_number('What is your 2nd marking period percentage? ')
w2 = get_number('What is the weighting of the 2nd marking period? ')
score = calculate_score((p1, p2), (w1, w2))
print(f'Your score is {score:.2f}%.')
def get_number(prompt):
while True:
try:
text = input(prompt)
except EOFError:
raise SystemExit()
else:
try:
number = float(text)
except ValueError:
print('Please enter a number.')
else:
break
return number
def calculate_score(percentages, weights):
if len(percentages) != len(weights):
raise ValueError('percentages and weights must have same length')
return sum(p * w for p, w in zip(percentages, weights)) / sum(weights)
if __name__ == '__main__':
main()
您应该编写函数以一次获取一个数字。如果在某个地方触发了at异常,则应该对其进行处理。请注意,下面显示的
get_number
函数将不断询问号码,但也会显示调用者指定的提示。如果您没有运行Python3.6或更高版本,则需要在main
函数中注释掉对print
的调用
#! /usr/bin/env python3
def main():
p1 = get_number('What is your 1st marking period percentage? ')
w1 = get_number('What is the weighting of the 1st marking period? ')
p2 = get_number('What is your 2nd marking period percentage? ')
w2 = get_number('What is the weighting of the 2nd marking period? ')
score = calculate_score((p1, p2), (w1, w2))
print(f'Your score is {score:.2f}%.')
def get_number(prompt):
while True:
try:
text = input(prompt)
except EOFError:
raise SystemExit()
else:
try:
number = float(text)
except ValueError:
print('Please enter a number.')
else:
break
return number
def calculate_score(percentages, weights):
if len(percentages) != len(weights):
raise ValueError('percentages and weights must have same length')
return sum(p * w for p, w in zip(percentages, weights)) / sum(weights)
if __name__ == '__main__':
main()
通过以下代码,您可以生成只接受整数值的函数:
def input_type(a):
if(type(10)==type(a)):
print("integer")
else:
print("not integer")
a=int(input())
input_type(a)
通过以下代码,您可以生成只接受整数值的函数:
def input_type(a):
if(type(10)==type(a)):
print("integer")
else:
print("not integer")
a=int(input())
input_type(a)
我无法访问主函数内部的局部变量。稍后我将尝试使用它们(p1、w1、p2、w2),但我无法回忆起它们,因为它们是在def()中本地定义的main函数您是否考虑过通过将局部变量作为参数提供给那些函数,从而将它们传递给以后要调用的函数?我无法访问main函数中的局部变量。稍后我将尝试使用它们(p1、w1、p2、w2),但我无法回忆起它们,因为它们是在def()主函数中本地定义的。您是否考虑过通过将局部变量作为参数提供给这些函数,从而将它们传递给稍后要调用的函数?