这是我当前的代码,当我运行它时,我得到一些错误…我不知道他们是什么。你知道吗
Traceback (most recent call last):
File "D:/.Actual W4/Lab3Problem1.py", line 54, in main()
File "D:/.Actual W4/Lab3Problem1.py", line 8, in main nestediffunction()
File "D:/.Actual W4/Lab3Problem1.py", line 27, in nestediffunction if (booksread == 0):
NameError: name 'booksread' is not defined
def main():
print("Welcome to Ryan's book club!")
booksread = int(input('Please enter the amount of books you have read so far: '))
# if (booksread < 0):
# print("That's impossible! Please re-enter the amount of books you have read: ")
# booksread1 = int(input('Please enter the amount of books you have read so far: '))
#simpleiffunction()
nestediffunction()
#eliffunction()
def simpleiffunction():
if (booksread == 0):
print('You have 0 points!')
if (booksread == 1):
print('You have 5 points!')
if (booksread == 2):
print('You have 15 points!')
if (booksread == 3):
print('You have 30 points!')
if (booksread >= 4):
print('You have 60 points!')
def nestediffunction():
if (booksread == 0):
print('You have 0 points!')
else:
if (booksread == 1):
print('You have 5 points!')
else:
if (booksread == 2):
print('You have 15 points!')
else:
if (booksread == 3):
print('You have 30 points!')
else:
if (booksread >= 4):
print('You have 60 points!')
def eliffunction():
if (booksread == 0):
print('You have 0 points!')
elif (booksread == 1):
print('You have 5 points!')
elif (booksread == 2):
print('You have 15 points!')
elif (booksread == 3):
print('You have 30 points!')
elif (booksread >= 4):
print('You have 60 points!')
main()
必须在主区域中传入booksread变量。这允许函数知道用户输入的书籍的编号。函数使用numbooksread变量存储传递的变量,以便在函数中使用。你知道吗
默认情况下,函数中的变量不是全局变量,因此其他函数中不存在该变量。您需要将其作为属性传递:
等等
变量
booksread
不在它使用的任何函数nestediffunction
、eliffunction
和simpleiffunction
的范围内。修改这两个函数,以便可以接受booksread
变量作为参数,并相应地从main传递它。你知道吗相关问题 更多 >
编程相关推荐