简体   繁体   中英

Calling a function with a returned integer multiple times in Python

I'm trying to make a Dungeons & Dragons program that will allow the user to roll dice when necessary. I created functions for each dice roll, since I have to use them multiple times, and it was just far easier.

The function:

import random
def roll20():
    roll20 = randint(1,20)
    return roll20

I created a while loop that theoretically allows the user to roll the dice as many times as they'd like (pardon the mess, the code is actually written for several types of dice, but I've cut it to only showing the d20 roll, although all present this error):

diceDecide = input("Would you like to roll a dice? 'Yes' or 'no'...")

while diceDecide.upper() != "YES" and diceDecide.upper() != "NO":
    diceDecide = input("Would you like to roll a dice? 'Yes' or 'no' only...")
while diceDecide.upper() == "YES":
    diceRoll = input("Select a dice to roll from the list above: ")
    if diceRoll == "d20":
        roll20 = roll20()
        print("\n",roll20)

This works perfectly when I run it the first time. However, if I attempt to roll the same dice twice, I get an error that says:

TypeError: 'int' object is not callable

I believe I know why the error is occurring - Python is reading the function call as the integer returned when it was previously ran - but I haven't the slightest clue how to fix it.

By using the name 'roll20' for your variable, you are overiding the function definition. Simply use a different name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM