简体   繁体   中英

Confused about if statements and functions

I'm very familiar with the random module but I always stumbled when it came to functions. How do I make a function only occur if it meets a certain condition? It gives me no output when im trying to validate my answer...

choice = input ("Which type of password would you like to generate? \n 1. Alphabetical \n")

if choice == 1:
    characters = list(string.ascii_letters)
    def generate_random_abc_password():
            length = int(input("Enter password length: "))

            random.shuffle(characters)
            
            password = []
            for i in range(length):
                    password.append(random.choice(characters))

            random.shuffle(password)

            print("".join(password))
            
    generate_random_abc_password()

Seems like the most common mistake among beginners. When you use an input() function, it will return you some number/text/float or decimal number but in string type. For example

x = input("Enter your number")
# If i would input 2, my x variable would contain "2"
# 3 => "3"
# 550 => "550" and so on...

To avoid such a problem and to store your value in proper type, you need to wrap your input with int() function, as shown below

x = int(input(("Enter your number"))
# From now, whenever i prompt any number from my keyboard, it will 
# be an integer number, which i can substract, add, multiply and divide.

As simple as it is. Happy learning!

You should convert the input to integer as by default the data type for input is string .

Alternatively rather than changing input data type you can compare it with the str(1) or change if choice == 1: to if choice == '1':

You can try using:

import string
import random

choice = int(input ("Which type of password would you like to generate? \n 1. Alphabetical \n"))

if choice == 1:
    characters = list(string.ascii_letters)
    def generate_random_abc_password():
            length = int(input("Enter password length: "))

            random.shuffle(characters)
            
            password = []
            for i in range(length):
                    password.append(random.choice(characters))

            random.shuffle(password)

            print("".join(password))
            
    generate_random_abc_password()

The above code will result in:

Which type of password would you like to generate? 
 1. Alphabetical 
1
Enter password length: 10
MEQyTcspdy

The problem here is that you've defined the characters variable in your if block. That is your function is not aware of of the variable that is why you should pass the characters variable as a input to the function the code should look like


choice = input ("Which type of password would you like to generate? \n 1. Alphabetical \n")

if choice == 1:
    characters = list(string.ascii_letters)
    def generate_random_abc_password(characters):
            length = int(input("Enter password length: "))

            random.shuffle(characters)
            
            password = []
            for i in range(length):
                    password.append(random.choice(characters))

            random.shuffle(password)

            print("".join(password))
            
    generate_random_abc_password(characters)


A function works like an isolated piece of code it takes it's own inputs and returns it's own outputs or does it's stuff.

As well as the input isn't casted to an int as the above mentioned answer says...

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