Python def function calling and random errors

Question:

I want to write a code that will display a warning msg that the user’s data has been encrypted (ransomware msg). In the msg, the name of the victim must be randomly selected from a list of names I have generated, and the amount to be paid also the same (100 to 1000). I have written the code but it is not running. How do you call a function after it has been made? And does my random code have errors that keeps it from running?

import random
def ransom_msg (name,ransom_amount):

    #Defining list of target names
    name = ["Daily Deal Retail","Healthline Medical","True Banking","HR solutions",
                 "Crypto Me", "Total Privacy", "Pineapple State University"]

    #Assigning payment range for random selection between 100 and 1000$
    ransom_amount = random.radint(100,1000)

    victim_name = random.choice(name)
    print(victim_name)
    victim_payment = random.choice(ransom_amount)
    print(victim_payment)
    print("please",victim_name,"pay us",victim_payment,"asap! Or else, all your data is forever lost.")

ransom_msg(victim_name,victim_payment)
Asked By: Muitinkinyakkin

||

Answers:

If you must use parameters, then it would be better to do something like my example below See inline notes for further explanation:

import random

def ransom_msg(names, victim_payment):  # function chooses a name randomly 
    victim_name = random.choice(names)  # and then prints the ransom message
    print("please", victim_name, "pay us", victim_payment,
          "asap! Or else, all your data is forever lost.")
     

if __name__ == "__main__":
    victim_names = ["Daily Deal Retail","Healthline Medical",
                "True Banking", "HR solutions", "Crypto Me", 
                "Total Privacy", "Pineapple State University"]
    victim_payment = random.radint(100,1000)   # chooses a random integer between 100 and 1000
    ransom_msg(victim_names, victim_payment)  # <-- correct usage of parameters

To further explain, if you need to use parameters in your function then those parameters need to come from somewhere outside of the function body. That is why my example defines the list of names and ransom value outside of the function so that those two objects can be used as the parameters.

Answered By: Alexander
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.