0

I was wondering how I would use the "adj" variable from def getInput and connect it to adjectives(), I am trying to make it so I can get input from the user and then have the random.choice activate depending on how many adjectives the user inputs. As a student for this project, I can only use user defined functions.

import random
def getInput():
    insult = input("Enter number of insults you want generated: ")
    target = input("Enter the targets name: ")
    adj = input("Enter number of adjectives you want in your insults: ")

def adjective():
    adjectives = ("aggressive", "cruel", "cynical", "deceitful", "foolish", "gullible", "harsh", "impatient", "impulsive", "moody", "narrow-minded", "obsessive", "ruthless", "selfish", "touchy")

    for adj in adjectives:
        print(random.choice(adjectives))
        break
  • Where do you want to use the value? Do you really need the `getInput` function? – OneCricketeer Oct 29 '16 at 16:47
  • Define ```adjective``` with an argument - ```def adjective(adj): ...```, return adj from ```getInput```, pass the return value to ```adjective```. https://docs.python.org/3/tutorial/controlflow.html#defining-functions – wwii Oct 29 '16 at 16:48
  • You can return "multiple" values: `return insult, target, adj` Its not *really* multiple values, its a tuple, but you can call the function like this: `insult, target, adj = getInput()`. – cdarke Oct 29 '16 at 17:02

2 Answers2

0

Here's one option.

import random
def getInput():
    insult = input("Enter number of insults you want generated: ")
    target = input("Enter the targets name: ")
    adj = input("Enter number of adjectives you want in your insults: ")
    return int(insult), int(target), int(adj) # cast to int and return 

def adjective(numAdj): # add a parameter 
    adjectives = ("aggressive", "cruel", "cynical", "deceitful", "foolish", "gullible", "harsh", "impatient", "impulsive", "moody", "narrow-minded", "obsessive", "ruthless", "selfish", "touchy")

    for i in range(numAdj): # use parameter
        print(random.choice(adjectives))
        # do not break the loop after one pass 

insult, target, adj = getInput() # get values 
adjective(adj) # pass in 

Another option is using global keyword within the function, or simply declaring your number values in the global scope to begin with

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

If you want to take the value of the "adj" variable in getInput() and use it in adjective(), you will want to return it from getInput() and you can then call getInput(). If you just add the line return adj at the end of getInput() then in another function - such as adjective() - you can use the assignment adj = getInput() to use it in that function.

In general you can return values from functions and pass values in as arguments to share values between functions - Python's documentation explains how this works: https://docs.python.org/2/tutorial/controlflow.html#defining-functions