Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在数组的特定输入小于或等于Python中的变量时向用户发出警报_Python_Python 3.x_For Loop - Fatal编程技术网

如何在数组的特定输入小于或等于Python中的变量时向用户发出警报

如何在数组的特定输入小于或等于Python中的变量时向用户发出警报,python,python-3.x,for-loop,Python,Python 3.x,For Loop,如果特定的完成时间等于常量记录,您将如何提醒用户。例如,“John”的成绩为9.76,低于当前世界纪录,然后打印命令会通知用户某名运动员已达到世界纪录。解释一下你的发现会很有帮助 import time datasets= [] world_record = int(9.76) for i in range(0, 8): print("Inputting Data for Lane", i) gender = str(input("Is the athlete male or

如果特定的完成时间等于常量记录,您将如何提醒用户。例如,“John”的成绩为9.76,低于当前世界纪录,然后打印命令会通知用户某名运动员已达到世界纪录。解释一下你的发现会很有帮助

import time
datasets= []
world_record = int(9.76)


for i in range(0, 8):
    print("Inputting Data for Lane", i)
    gender = str(input("Is the athlete male or female ")) 
    athlete = str(input("What is the athletes name "))
    finishTime = float(input("What was the finishing time "))
    dataset = [gender, athlete, finishTime]
    datasets.append(dataset)
    if finishTime == float("10"):
        print("A world record has been reached")

print("{0:<10}{1:<10}{2:<15}".format("Gender","Athlete","Finish time"))

ds = sorted(datasets, key=lambda x:x[2], reverse=False)

for s in ds:
    time.sleep(1)
    print("{0:<10}{1:<10}{2:<15}".format(s[0], s[1], s[2]))
导入时间
数据集=[]
世界纪录=int(9.76)
对于范围(0,8)内的i:
打印(“输入车道数据”,i)
性别=str(输入(“运动员是男性还是女性”))
运动员=str(输入(“运动员姓名”))
finishTime=float(输入(“完成时间是多少”))
数据集=[性别、运动员、完成时间]
附加(数据集)
如果finishTime==浮动(“10”):
印刷品(“已达到世界纪录”)
打印(“{0:
world\u record=int(9.76)
四舍五入为9,保留为
world\u record=9.76

然后检查完成时间是否小于记录

for i in range(0, 8):
    print("Inputting Data for Lane", i)
    gender = str(input("Is the athlete male or female ")) 
    athlete = str(input("What is the athletes name "))
    finishTime = float(input("What was the finishing time "))
    dataset = [gender, athlete, finishTime]
    datasets.append(dataset)
    #check if the world record has been met
    if finishTime == world_record:
        print("A world record has been met")
    elif finishTime > world_record:
        print("A world record has been beaten")
        print("the new world record is {}".format(s[2]))
        world_record = finishTime 

ds中的s的
:
时间。睡眠(1)

打印(“{0:我应该提到,我已经尝试过这个方法,我将如何对数组中的一组特定数据执行此操作。因为数组中最多有8个条目。为了澄清,您想检查是否有一个特定的人打破了记录,或者您想检查是否有人打破了记录?如果是一个特定的人,您知道他们在数组中的索引吗或者他们的名字?我想让它在任何人打破记录时提醒用户,并告诉用户他们的名字。例如,John打破了当前的世界记录。答案已经更新,另一方面,input()在python3中返回一个字符串,因此您不需要用str()括起来该代码仅影响上次输入的完成时间
for s in ds:
    time.sleep(1)
    print("{0:<10}{1:<10}{2:<15}".format(s[0], s[1], s[2]))
    if finishTime == world_record:
        print("A world record has been met")
    elif finishTime > world_record:
        print("A world record has been beaten")
        print("the new world record is {}".format(s[2]))
        world_record = finishTime