[英]What am I doing wrong in my College class program
So I am trying to get a program working for my class.所以我试图让一个程序为我的班级工作。 I have to create a random number and then have it print off and from there I have to have it list the highest number and the lowest number.我必须创建一个随机数,然后将其打印出来,然后我必须让它列出最高数字和最低数字。 I am struggling to figure out how to get that to work.我正在努力弄清楚如何让它发挥作用。
import random
print("My Favorite Numbers")
for myFavNumbers in range(0,100,10):
myFavNumbers = random.randint(0,100)
print(myFavNumbers)
numSrch = (myFavNumbers)
for myFavNumbers in range(myFavNumbers):
if numSrch[myFavNumbers].upper()==numSrch.upper():
print(myFavNumbers[myFavNumbers])
I hope I understood your task correctly.我希望我正确理解你的任务。 Is highest = max?最高=最高? Is lowest = min?最低=分钟?
import random
random_numbers = []
print('My random numbers:')
for _ in range(10):
random_numbers.append(random.randint(0, 100))
print(random_numbers[-1])
print()
print(f'Max one: {max(random_numbers)}')
print(f'Min one: {min(random_numbers)}')
Try this Code:试试这个代码:
import random
print("My Favorite Numbers")
listofstud=[]
for myFavNumbers in range(0,100,10):
myFavNumbers = random.randint(0,100)
listofstud.append(myFavNumbers)
print("List of Student:"+str(listofstud))
print("Max Number:"+str(max(listofstud)))
print("Max Number:"+str(min(listofstud)))
this code will give max and min num from list此代码将从列表中给出最大值和最小值
From what I understand, your assignment is asking you to:据我了解,您的任务要求您:
Right now, what your code is doing is:现在,您的代码正在做的是:
Rather than saving the last number, you need to find a way record each number as it passes by.您需要找到一种方法来记录每个经过的数字,而不是保存最后一个数字。 That is, you need to store the numbers in a list.也就是说,您需要将数字存储在列表中。
Once you've built a list of random numbers, you can use Python's built-in functions to do the rest of the work for you.一旦您构建了一个随机数列表,您就可以使用 Python 的内置函数来为您完成剩下的工作。
So, given all that, see if this code makes sense:因此,考虑到所有这些,看看这段代码是否有意义:
import random
print("My Favorite Numbers")
favoriteNumberList = []
for _ in range(0,100,10):
myFavNumber = random.randint(0,100)
favoriteNumberList.append(myFavNumber)
print(favoriteNumberList)
print(max(favoriteNumberList))
print(min(favoriteNumberList))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.