我该怎么做才能有没有符号的数字?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我该怎么做才能有没有符号的数字?相关的知识,希望对你有一定的参考价值。
我的代码有什么问题?我是在几周前开始使用python的,我正在尝试制作一个可定制的随机密码生成器。我想为用户提供使其完全大写,完全小写或正常(每个单词开头都大写)的选项。我也希望他们可以选择是否包含数字或符号。每次运行该程序时,都不会让我没有数字的符号。怎么了?
对刚刚回答说要显示错误和内容的人,我实际上只是报名参加堆栈溢出,它说我不能上传图片,但我该怎么做
import random
def password():
normal_adjectives = ['Funny', 'Amazing', 'Infinity', 'Fabulous', 'Red', 'Rainbow', 'Adorable', 'Adventurous', 'Impressive', 'Determined',
'Delighted', 'Scary', 'Active', 'Distinct', 'Eager', 'Elegant', 'Excited', 'Enchanted', 'Fair', 'Fierce', 'Fancy',
'Gentle', 'Glamorous', 'Graceful', 'Happy', 'Handsome', 'Hungry', 'Fluffy', 'Important', 'Itchy', 'Impossible',
'Jealous', 'Joyous', 'Jolly', 'Kind', 'Caring', 'Lazy', 'Lonely', 'Lucky', 'Lovely', 'Magnificent', 'Modern',
'Mysterious', 'Naughty', 'Nervous', 'Nice', 'Obnoxious', 'Outrageous', 'Outstanding', 'Perfect', 'Pleasant',
'Powerful', 'Precious', 'Proud', 'Real', 'Rich', 'Smily', 'Sleepy', 'Sore', 'Sparkling', 'Splendid',
'Stormy', 'Strange', 'Super', 'Talented', 'Tense', 'Tender', 'Terrible', 'Thankful', 'Thoughtful',
'Tired', 'Tough', 'Troubled', 'Unusual', 'Vast', 'Victorious', 'Vicious', 'Slick', 'Weary', 'Wicked',
'Wild', 'Worried', 'Zealous', 'Orange', 'Yellow', 'Green', 'Blue', 'Pink', 'Purple', 'Indigo', 'Violet',
'Affectionate', 'Stubborn', 'Confused', 'Sticky', 'Slimy', 'Hairless', 'Hairy']
normal_nouns = ['Cheetah', 'Book', 'Chair', 'Pigeon', 'Dolphin', 'Actor', 'Animal', 'Apple', 'Balloon', 'Banana', 'Battery',
'Bed', 'Boy', 'Branch', 'Car', 'Candle', 'Carpet', 'Crayon', 'Diamond', 'Dream', 'Dress', 'Egg', 'Eggplant',
'Elephant', 'Eye', 'Fish', 'Forest', 'Football', 'Fountain', 'Garage', 'Gold', 'Grass', 'Guitar', 'Hamburger',
'Helmet', 'Horse', 'Penguin', 'Honey', 'Ice', 'Insect', 'Iron', 'Jelly', 'Juice', 'Kangaroo', 'Kite', 'Knife',
'Lamp', 'Lion', 'Lizard', 'Lunch', 'Magazine', 'Monkey', 'Microphone', 'Nail', 'Needle', 'Notebook', 'Ocean',
'Oil', 'Oyster', 'Ghost', 'Parrot', 'Piano', 'Pizza', 'Pillow', 'Planet', 'Queen', 'Rainbow', 'Restaurant',
'River', 'Rose', 'Sandwich', 'Shampoo', 'Sugar', 'Stone', 'Spoon', 'Shoe', 'Phone', 'Telephone', 'Television',
'Tent', 'Tomato', 'Toothbrush', 'Train', 'Truck', 'Umbrella', 'Vase', 'Vegetable', 'Vulture', 'Wall', 'Whale',
'Wizard', 'Window', 'Wire', 'Xylophone', 'Yacht', 'Zebra', 'Glass', 'Gas', 'Budgie', 'Snake', 'Chimpanzee']
funny_adjectives = ['Hairy', 'Crazy', 'Hysterical', 'Funny', 'Feisty', 'Chunky', 'Fluffy', 'Hungry', 'Itchy', 'Jolly',
'Modern', 'Naughty', 'Obnoxious', 'Outrageous', 'Smily', 'Troubled', 'Wild', 'Brown', 'Hairless',
'Slimy', 'Sticky', 'Confused', 'Stubborn', 'Idiotic', 'Affectionate', 'Determined', 'Feeble'
'Flying', 'Helpless', 'Hopeful', 'Dead']
funny_nouns = ['Monkey', 'Toaster', 'Lettuce', 'Kitty', 'ElasticBand', 'Arms', 'Legs', 'ToiletSeat', 'DeathWish',
'Pigeon', 'Egg', 'Grass', 'Hamburger', 'Shampoo', 'Door', 'Wall', 'FishEye', 'FluffBall', 'Sardine',
'Blubber', 'Gargoyle', 'Goggles', 'Hedge', 'Shrub', 'Squelch', 'Donkey', 'Pepper']
animal_adjectives = ['Flying', 'Epic', 'Hissing', 'Swift', 'Fast', 'Amazing', 'Growling', 'Snarling', 'Frisky',
'Amphibious', 'Exotic', 'Feral', 'Wild', 'Dead', 'Scary', 'Fluffy', 'Domestic', 'Tame',
'Vicious', 'Graceful', 'Predatory', 'Aquatic', 'Screeching', 'Adorable']
animal_nouns = ['Cat', 'Snake', 'Dog', 'Monkey', 'Budgie', 'Bird', 'Ant', 'Anteater', 'Fish', 'Sardine', 'Chimpanzee',
'Clownfish', 'Lionfish', 'Bear', 'Lion', 'Tiger', 'Elephant', 'Hippo', 'Rhinoceros', 'Frog', 'Wolf',
'Buffalo', 'Eagle', 'Hawk', 'Albatross', 'Lynx', 'PolarBear', 'Snowcat', 'Lizard', 'Crocodile',
'Alligator', 'Parrot', 'Badger', 'Beaver', 'Crow', 'Squirrel', 'Dragon', 'Cheetah', 'Pigeon',
'Dolphin', 'Kangaroo', 'Fly', 'Insect', 'Horse', 'Penguin', 'Squid', 'Octopus', 'Vulture',
'Zebra', 'Kitten', 'Donkey', 'Antelope', 'Echidna', 'Pelican', 'Baboon', 'Bee', 'Swan', 'Tapir',
'Hyena', 'Lemur', 'Pig', 'Cow', 'Sheep', 'Camel', 'Giraffe', 'Cobra', 'Crab', 'Crane', 'Dingo',
'Fox', 'Dove', 'JayBird', 'Owl', 'Hedgehog', 'Elk', 'Emu', 'Falcon', 'Turtle', 'Hornet', 'Wasp',
'Ibis', 'Jackal', 'Koala']
landmarks_adjectives = ['Tall', 'Sparkling', 'Dazzling', 'White', 'Sandy', 'Rocky', 'Big', 'Large', 'Golden', 'Amazing',
'Inccredible']
landmarks_nouns = ['EiffelTower', 'TowerOfPisa', 'Colosseum', 'WhiteHouse', 'TajMahal', 'WallOfChina', 'StatueOfLiberty',
'PyramidOfGiza', 'Uluru', 'OperaHouse', 'ParliamentHouse', 'LondonEye', 'Stonehenge',
'GoldenGateBridge']
food_adjectives = ['Delicious', 'Juicy', 'Luxurious', 'Warm', 'Salty', 'Spicy', 'Flavoursome', 'Bitter', 'Enjoyable',
'Delectable', 'Crunchy', 'Sweet', 'Sour', 'Savoury', 'Divine', 'Fruity', 'Appetising', 'Heavenly',
'Hot', 'Lush', 'Mouthwatering', 'Incredible', 'Horrid', 'Peppery', 'Pungent', 'Pleasant',
'Rich', 'Scrumptious', 'Sharp', 'Strong', 'Succulent', 'Sugary', 'Saline', 'Tasteless', 'Tasty',
'Yummy']
food_nouns = ['Apple', 'Banana', 'Hamburger', 'Burger', 'Cheeseburger', 'Strawberry', 'Steak', 'Pasta', 'Mince',
'Fry', 'Chip', 'Avocado', 'Blueberry', 'Orange', 'Egg', 'Chicken', 'Coconut', 'Asparagus', 'Broccoli',
'Cauliflower', 'Carrot', 'Cucumber', 'Garlic', 'Kale', 'Tomato', 'Salmon', 'Cereal', 'Fruit',
'Bread', 'Cookie', 'Cheese', 'Yoghurt', 'Milk', 'Juice', 'Eggplant', 'Jelly', 'Honey', 'Pizza']
harrypotter_adjectives = ['Magical', 'Swish', 'Flick', 'Transforming', 'Brave', 'Bold', 'Invisible', 'Dead', 'Forbidden',
'Mysterious', 'Secret']
harrypotter_nouns = ['HarryPotter', 'Dumbledore', 'Snape', 'Weasley', 'Hermione', 'McGonagall', 'SiriusBlack',
'Hagrid', 'Hedwig', 'Dobby', 'Voldemort', 'Mad-EyeMoody', 'Malfoy', 'Lovegood', 'Longbottom',
'DeathlyHallows', 'Umbridge', 'Lupin', 'Scamander', 'Nagini', 'Lestrange', 'Dursley', 'Tonks',
'Transfiguration', 'Gryffindor', 'Ravenclaw', 'Slytherin', 'Hufflepuff', 'Hogwarts', 'Magic']
cats_adjectives = ['Fuzzy', 'Furry', 'Agile', 'Clever', 'Precise', 'Patient', 'Hunter', 'Loud', 'Lethal', 'Lazy',
'Predatory', 'Energetic', 'Sleepy', 'Loyal', 'Adorable', 'Cute', 'Passionate']
cats_nouns = ['Feline', 'Cat', 'Meow', 'Purr', 'Hiss', 'Persian', 'Bengal', 'MaineCoon', 'Siamese', 'Sphynx', 'Ragdoll',
'Abyssinian']
dogs_adjectives = ['Friendly', 'Loyal', 'Loud', 'Adorable', 'Cute', 'Passionate', 'Strong', 'Smelly', 'Protective',
'Smart', 'Obedient']
dogs_nouns = ['Woof', 'Dog', 'GermanShepherd', 'Labrador', 'Retriever', 'Husky', 'Poodle', 'Bulldog', 'Pug', 'Chihuahua',
'Rottweiler', 'Maltese', 'Terrier', 'ChowChow']
numbers = random.randint(1,100)
symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '\'', '-', '_', '+', '=', '`', '~', '[', '', ']', '', '|',
'<', '.', '>', '/', '?']
result_normal = random.choice(normal_adjectives) + random.choice(normal_nouns)
result_funny = random.choice(funny_adjectives) + random.choice(funny_nouns)
result_animal = random.choice(animal_adjectives) + random.choice(animal_nouns)
result_landmarks = random.choice(landmarks_adjectives) + random.choice(landmarks_nouns)
result_food = random.choice(food_adjectives) + random.choice(food_nouns)
result_potter = random.choice(harrypotter_adjectives) + random.choice(harrypotter_nouns)
result_cats = random.choice(cats_adjectives) + random.choice(cats_nouns)
result_dogs = random.choice(dogs_adjectives) + random.choice(dogs_nouns)
case = input('Would you like your password in lowercase, uppercase or normal? (lower, upper, normal) ')
number_want = input('Do you want numbers? y/n ')
symbol_want = input('Do you want symbols? y/n ')
theme = input('Enter in the theme for your password (normal/funny/animal/landmarks/food/harry potter/cats/dogs) ')
if case == 'upper':
result_normal = result_normal.upper()
result_funny = result_funny.upper()
result_animal = result_animal.upper()
result_landmarks = result_landmarks.upper()
result_food = result_food.upper()
result_potter = result_potter.upper()
result_cats = result_cats.upper()
result_dogs = result_dogs.upper()
if case == 'lower':
result_normal = result_normal.lower()
result_funny = result_funny.lower()
result_animal = result_animal.lower()
result_landmarks = result_landmarks.lower()
result_food = result_food.lower()
result_potter = result_potter.lower()
result_cats = result_cats.lower()
result_dogs = result_dogs.lower()
if number_want == 'y':
result_normal = result_normal + str(numbers)
result_funny = result_funny + str(numbers)
result_animal = result_animal + str(numbers)
result_landmarks = result_landmarks + str(numbers)
result_food = result_food + str(numbers)
result_potter = result_potter + str(numbers)
result_cats = result_cats + str(numbers)
result_dogs = result_dogs + str(numbers)
if symbol_want == 'y':
result_normal = result_normal + random.choice(symbols)
result_funny = result_funny + random.choice(symbols)
result_animal = result_animal + random.choice(symbols)
result_landmarks = result_landmarks + random.choice(symbols)
result_food = result_food + random.choice(symbols)
result_potter = result_potter + random.choice(symbols)
result_cats = result_cats + random.choice(symbols)
result_dogs = result_dogs + random.choice(symbols)
if theme=='normal':
print(result_normal)
print('Your new password is ' + result_normal)
elif theme=='funny':
print(result_funny)
print('Your new password is ' + result_funny)
elif theme=='animal':
print(result_animal)
print('Your new password is ' + result_animal)
elif theme=='landmarks':
print(result_landmarks)
print('Your new password is ' + result_landmarks)
elif theme=='food':
print(result_food)
print('Your new password is ' + result_food)
elif theme=='harry potter':
print(result_potter)
print('Your new password is ' + result_potter)
elif theme=='cats':
print(result_cats)
print('Your new password is ' + result_cats)
elif theme=='dogs':
print(result_dogs)
print('Your new password is ' + result_dogs)
else:
print(theme + ' is not a valid theme!')
other_p = input('Would you like another password? y/n ')
if other_p == 'y':
password()
start = input('Welcome to the Python Password Generator! Type the word start to start! ')
if start == 'start':
password()
else:
print('Type \'start\' to start, not ' + start)
答案
这是一个缩进错误。在最后打印密码的if语句位于symbol_want if语句内部。您需要将打印语句缩进一级,它将正常工作。
import random
def password():
normal_adjectives = ['Funny', 'Amazing', 'Infinity', 'Fabulous', 'Red', 'Rainbow', 'Adorable', 'Adventurous', 'Impressive', 'Determined',
'Delighted', 'Scary', 'Active', 'Distinct', 'Eager', 'Elegant', 'Excited', 'Enchanted', 'Fair', 'Fierce', 'Fancy',
'Gentle', 'Glamorous', 'Graceful', 'Happy', 'Handsome', 'Hungry', 'Fluffy', 'Important', 'Itchy', 'Impossible',
'Jealous', 'Joyous', 'Jolly', 'Kind', 'Caring', 'Lazy', 'Lonely', 'Lucky', 'Lovely', 'Magnificent', 'Modern',
'Mysterious', 'Naughty', 'Nervous', 'Nice', 'Obnoxious', 'Outrageous', 'Outstanding', 'Perfect', 'Pleasant',
'Powerful', 'Precious', 'Proud', 'Real', 'Rich', 'Smily', 'Sleepy', 'Sore', 'Sparkling', 'Splendid',
'Stormy', 'Strange', 'Super', 'Talented', 'Tense', 'Tender', 'Terrible', 'Thankful', 'Thoughtful',
'Tired', 'Tough', 'Troubled', 'Unusual', 'Vast', 'Victorious', 'Vicious', 'Slick', 'Weary', 'Wicked',
'Wild', 'Worried', 'Zealous', 'Orange', 'Yellow', 'Green', 'Blue', 'Pink', 'Purple', 'Indigo', 'Violet',
'Affectionate', 'Stubborn', 'Confused', 'Sticky', 'Slimy', 'Hairless', 'Hairy']
normal_nouns = ['Cheetah', 'Book', 'Chair', 'Pigeon', 'Dolphin', 'Actor', 'Animal', 'Apple', 'Balloon', 'Banana', 'Battery',
'Bed', 'Boy', 'Branch', 'Car', 'Candle', 'Carpet', 'Crayon', 'Diamond', 'Dream', 'Dress', 'Egg', 'Eggplant',
'Elephant', 'Eye', 'Fish', 'Forest', 'Football', 'Fountain', 'Garage', 'Gold', 'Grass', 'Guitar', 'Hamburger',
'Helmet', 'Horse', 'Penguin', 'Honey', 'Ice', 'Insect', 'Iron', 'Jelly', 'Juice', 'Kangaroo', 'Kite', 'Knife',
'Lamp', 'Lion', 'Lizard', 'Lunch', 'Magazine', 'Monkey', 'Microphone', 'Nail', 'Needle', 'Notebook', 'Ocean',
'Oil', 'Oyster', 'Ghost', 'Parrot', 'Piano', 'Pizza', 'Pillow', 'Planet', 'Queen', 'Rainbow', 'Restaurant',
'River', 'Rose', 'Sandwich', 'Shampoo', 'Sugar', 'Stone', 'Spoon', 'Shoe', 'Phone', 'Telephone', 'Television',
'Tent', 'Tomato', 'Toothbrush', 'Train', 'Truck', 'Umbrella', 'Vase', 'Vegetable', 'Vulture', 'Wall', 'Whale',
'Wizard', 'Window', 'Wire', 'Xylophone', 'Yacht', 'Zebra', 'Glass', 'Gas', 'Budgie', 'Snake', 'Chimpanzee']
funny_adjectives = ['Hairy', 'Crazy', 'Hysterical', 'Funny', 'Feisty', 'Chunky', 'Fluffy', 'Hungry', 'Itchy', 'Jolly',
'Modern', 'Naughty', 'Obnoxious', 'Outrageous', 'Smily', 'Troubled', 'Wild', 'Brown', 'Hairless',
'Slimy', 'Sticky', 'Confused', 'Stubborn', 'Idiotic', 'Affectionate', 'Determined', 'Feeble'
'Flying', 'Helpless', 'Hopeful', 'Dead']
funny_nouns = ['Monkey', 'Toaster', 'Lettuce', 'Kitty', 'ElasticBand', 'Arms', 'Legs', 'ToiletSeat', 'DeathWish',
'Pigeon', 'Egg', 'Grass', 'Hamburger', 'Shampoo', 'Door', 'Wall', 'FishEye', 'FluffBall', 'Sardine',
'Blubber', 'Gargoyle', 'Goggles', 'Hedge', 'Shrub', 'Squelch', 'Donkey', 'Pepper']
animal_adjectives = ['Flying', 'Epic', 'Hissing', 'Swift', 'Fast', 'Amazing', 'Growling', 'Snarling', 'Frisky',
'Amphibious', 'Exotic', 'Feral', 'Wild', 'Dead', 'Scary', 'Fluffy', 'Domestic', 'Tame',
'Vicious', 'Graceful', 'Predatory', 'Aquatic', 'Screeching', 'Adorable']
animal_nouns = ['Cat', 'Snake', 'Dog', 'Monkey', 'Budgie', 'Bird', 'Ant', 'Anteater', 'Fish', 'Sardine', 'Chimpanzee',
'Clownfish', 'Lionfish', 'Bear', 'Lion', 'Tiger', 'Elephant', 'Hippo', 'Rhinoceros', 'Frog', 'Wolf',
'Buffalo', 'Eagle', 'Hawk', 'Albatross', 'Lynx', 'PolarBear', 'Snowcat', 'Lizard', 'Crocodile',
'Alligator', 'Parrot', 'Badger', 'Beaver', 'Crow', 'Squirrel', 'Dragon', 'Cheetah', 'Pigeon',
'Dolphin', 'Kangaroo', 'Fly', 'Insect', 'Horse', 'Penguin', 'Squid', 'Octopus', 'Vulture',
'Zebra', 'Kitten', 'Donkey', 'Antelope', 'Echidna', 'Pelican', 'Baboon', 'Bee', 'Swan', 'Tapir',
'Hyena', 'Lemur', 'Pig', 'Cow', 'Sheep', 'Camel', 'Giraffe', 'Cobra', 'Crab', 'Crane', 'Dingo',
'Fox', 'Dove', 'JayBird', 'Owl', 'Hedgehog', 'Elk', 'Emu', 'Falcon', 'Turtle', 'Hornet', 'Wasp',
'Ibis', 'Jackal', 'Koala']
landmarks_adjectives = ['Tall', 'Sparkling', 'Dazzling', 'White', 'Sandy', 'Rocky', 'Big', 'Large', 'Golden', 'Amazing',
'Inccredible']
landmarks_nouns = ['EiffelTower', 'TowerOfPisa', 'Colosseum', 'WhiteHouse', 'TajMahal', 'WallOfChina', 'StatueOfLiberty',
'PyramidOfGiza', 'Uluru', 'OperaHouse', 'ParliamentHouse', 'LondonEye', 'Stonehenge',
'GoldenGateBridge']
food_adjectives = ['Delicious', 'Juicy', 'Luxurious', 'Warm', 'Salty', 'Spicy', 'Flavoursome', 'Bitter', 'Enjoyable',
'Delectable', 'Crunchy', 'Sweet', 'Sour', 'Savoury', 'Divine', 'Fruity', 'Appetising', 'Heavenly',
'Hot', 'Lush', 'Mouthwatering', 'Incredible', 'Horrid', 'Peppery', 'Pungent', 'Pleasant',
'Rich', 'Scrumptious', 'Sharp', 'Strong', 'Succulent', 'Sugary', 'Saline', 'Tasteless', 'Tasty',
'Yummy']
food_nouns = ['Apple', 'Banana', 'Hamburger', 'Burger', 'Cheeseburger', 'Strawberry', 'Steak', 'Pasta', 'Mince',
'Fry', 'Chip', 'Avocado', 'Blueberry', 'Orange', 'Egg', 'Chicken', 'Coconut', 'Asparagus', 'Broccoli',
'Cauliflower', 'Carrot', 'Cucumber', 'Garlic', 'Kale', 'Tomato', 'Salmon', 'Cereal', 'Fruit',
'Bread', 'Cookie', 'Cheese', 'Yoghurt', 'Milk', 'Juice', 'Eggplant', 'Jelly', 'Honey', 'Pizza']
harrypotter_adjectives = ['Magical', 'Swish', 'Flick', 'Transforming', 'Brave', 'Bold', 'Invisible', 'Dead', 'Forbidden',
'Mysterious', 'Secret']
harrypotter_nouns = ['HarryPotter', 'Dumbledore', 'Snape', 'Weasley', 'Hermione', 'McGonagall', 'SiriusBlack',
'Hagrid', 'Hedwig', 'Dobby', 'Voldemort', 'Mad-EyeMoody', 'Malfoy', 'Lovegood', 'Longbottom',
'DeathlyHallows', 'Umbridge', 'Lupin', 'Scamander', 'Nagini', 'Lestrange', 'Dursley', 'Tonks',
'Transfiguration', 'Gryffindor', 'Ravenclaw', 'Slytherin', 'Hufflepuff', 'Hogwarts', 'Magic']
cats_adjectives = ['Fuzzy', 'Furry', 'Agile', 'Clever', 'Precise', 'Patient', 'Hunter', 'Loud', 'Lethal', 'Lazy',
'Predatory', 'Energetic', 'Sleepy', 'Loyal', 'Adorable', 'Cute', 'Passionate']
cats_nouns = ['Feline', 'Cat', 'Meow', 'Purr', 'Hiss', 'Persian', 'Bengal', 'MaineCoon', 'Siamese', 'Sphynx', 'Ragdoll',
'Abyssinian']
dogs_adjectives = ['Friendly', 'Loyal', 'Loud', 'Adorable', 'Cute', 'Passionate', 'Strong', 'Smelly', 'Protective',
'Smart', 'Obedient']
dogs_nouns = ['Woof', 'Dog', 'GermanShepherd', 'Labrador', 'Retriever', 'Husky', 'Poodle', 'Bulldog', 'Pug', 'Chihuahua',
'Rottweiler', 'Maltese', 'Terrier', 'ChowChow']
numbers = random.randint(1,100)
symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '\'', '-', '_', '+', '=', '`', '~', '[', '', ']', '', '|',
'<', '.', '>', '/', '?']
result_normal = random.choice(normal_adjectives) + random.choice(normal_nouns)
result_funny = random.choice(funny_adjectives) + random.choice(funny_nouns)
result_animal = random.choice(animal_adjectives) + random.choice(animal_nouns)
result_landmarks = random.choice(landmarks_adjectives) + random.choice(landmarks_nouns)
result_food = random.choice(food_adjectives) + random.choice(food_nouns)
result_potter = random.choice(ha以上是关于我该怎么做才能有没有符号的数字?的主要内容,如果未能解决你的问题,请参考以下文章