1 回答

TA貢獻1797條經驗 獲得超4個贊
您必須 使用 才能訪問 中的數字。你應該使用而不是在self.numberupdateselfnumberdef update(number)
此外,要創建新列表,您必須在 -loop 之前創建空列表并創建到此列表。稍后,您可以將此列表獲取為forappend(newnum)returnb = ...
class Fraction():
def __init__(self):
shape = int(input("How many sides does the shape have? : "))
if shape <= 0: #doesnt work with negatives?
print("Please make a valid choice (positive integers only)")
shape = int(input("How many sides does the shape have? : "))
self.numbers = []
for i in range(shape):
n = random.randint(1, 100)
self.numbers.append(n)
print("Your numbers are:", self.numbers)
def update(self):
newlist = []
long = len(self.numbers)
for i in range(long-1): # it has to be long-1 because later `long-1+1`will give `long`
newnum = self.numbers[i] - self.numbers[i+1]
newlist.append(newnum)
return newlist
import random
def main():
a = Fraction()
b = a.update()
print(b)
main()
順便說一句:最好使用外部類并運行它,以便它可以使用來自或文件或硬編碼的變量運行。使用相同的值一次又一次地測試代碼是有幫助的。input()Fraction(shape)input()
添加回答
舉報