我已经开始了python课程,但我的书似乎对我没有帮助。我的教授有一个程序,用不同的输入轰炸我的代码,如果任何输入不起作用,则我的代码“错误”。我已经做了很多天的编辑工作,完全不知所措。如果有人输入并输入实际数字,我的代码将起作用。但是我的代码未能通过测试的地方是输入是否为“ miles_to_laps(26)”,它会出错。
我尝试将输入更改为int(input()),但这不能解决问题。我经历了更改变量甚至更改输入法的过程,但仍然茫然。我已经尝试联系我的老师,但是6天没有任何回应,而3天都迟到了,我觉得我什么也没去。
user_miles = int(input())
def miles_to_laps(user_miles):
x = user_miles
y = 4
x2 = x * y
result = print('%0.2f' % float(x2))
return result
miles_to_laps(user_miles)
我的代码适用于实数输入,但是我的教授希望使用Miles_to_laps(26)和Miles_to_laps(13)之类的输入来创建相同的输出。
对于奇怪的输入功能,您可以尝试:
import re
def parse_function_text(s):
try:
return re.search("miles_to_laps\((.+)\)", s)[1]
except TypeError:
return None
def accept_input(user_input):
desugar = parse_function_text(user_input)
if desugar is not None:
user_input = desugar
try:
return float(user_input)
except ValueError:
raise ValueError("Cannot process input %s" % user_input)
assert accept_input("miles_to_laps(3.5)") == 3.5
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句