简体   繁体   English

如何在python的for循环中仅打印某些条件

[英]How to print only certain conditions inside my for loop in python

so I am trying to make a for loop to print a certain condition but it keeps printing all the conditions. 所以我试图做一个for循环来打印某个条件,但它会继续打印所有条件。 How do I make it stop and print the outcome I want? 如何使其停止并打印所需的结果? Here's what I have done till now: 到目前为止,这是我所做的:

f_list = ['nothing','You will get an A', 'You will learn to program','You are destined to become a master coder','May the force be with you \n',
    'Wow. You look like a programmer','The code is strong with this one','You will be testing your code often','Taco cat spelled backwards is taco cat','Your name will go down, down, down in history \n',
    'You will master python']

for luckyInt in range(1 ,len(f_list)):
    if (luckyInt == 1):
        print(f_list[1])
    elif luckyInt == 2:
        print(f_list[2])
    elif luckyInt == 3:
        print(f_list[3])
    elif luckyInt == 4:
        print(f_list[4])
    elif luckyInt == 5:
        print(f_list[5])
    elif luckyInt == 6:
        print(f_list[6])
    elif luckyInt == 7:
        print(f_list[7])
    elif luckyInt == 8:
        print(f_list[8])
    elif luckyInt == 9:
        print(f_list[9])
    elif luckyInt == 10:
        print(f_list[10])

Hi: The required output is this: Ooh-de-la-lay! 嗨:所需的输出是这样的:Ooh-de-la-lay! Ooh-de-la-lay! 哦,德拉打好! Fortune tellers! 算命先生! Fortunes forecast! 运势预测! Lucky charms! 幸运符! Hmmm… the future is cloudy. 嗯...未来是阴暗的。 What is your name?Leia Organa Oo-dee-lally! 你叫什么名字?Leia Organa Oo-dee-lally! How exciting! 多么激动人心! How many fortunes do you wish to get today?5 Your total for today's session will be: 10.8455 I will tell your fortune. 您希望今天获得多少运气?5今天的会议总费用为:10.8455我将告诉您。 Leia Organa, enter your lucky integer number.300 Oops Leia Organa. Leia Organa,输入您的幸运整数300糟糕的Leia Organa。 I cannot tell your fortune until you enter a valid number.99 Leia Organa! 在您输入有效的数字之前,我无法说出您的命运。99Leia Organa! This is your lucky day! 这是你的幸运日! Your name will go down, down, down in history I will tell your fortune. 你的名字会跌宕起伏,我会告诉你的命运。 Leia Organa, enter your lucky integer number.-3 Oops Leia Organa. Leia Organa,输入您的幸运整数。-3糟糕,Leia Organa。 I cannot tell your fortune until you enter a valid number.3 Leia Organa! 在您输入有效数字之前,我无法说出您的命运。3Leia Organa! This is your lucky day! 这是你的幸运日! You are destined to become a master coder I will tell your fortune. 您注定要成为一名高级编码员,我将告诉您。 Leia Organa, enter your lucky integer number.0 Leia Organa! Leia Organa,输入您的幸运整数。0Leia Organa! This is your lucky day! 这是你的幸运日! You will master python I will tell your fortune. 您将掌握python,我将告诉您您的运气。 Leia Organa, enter your lucky integer number.-7 Oops Leia Organa. Leia Organa,输入您的幸运整数。-7糟糕,Leia Organa。 I cannot tell your fortune until you enter a valid number.7 Leia Organa! 在您输入有效数字之前,我无法说出您的命运。7Leia Organa! This is your lucky day! 这是你的幸运日! You will be testing your code often I will tell your fortune. 我经常告诉您,您将经常测试您的代码。 Leia Organa, enter your lucky integer number.50000 Oops Leia Organa. Leia Organa,输入您的幸运整数。50000糟糕Leia Organa。 I cannot tell your fortune until you enter a valid number.100 Leia Organa! 输入有效数字之前,我无法说出您的命运。100Leia Organa! This is your lucky day! 这是你的幸运日! You will master python You entered the following lucky numbers: [99, 3, 0, 7, 100] Process finished with exit code 0 : 您将掌握python您输入了以下幸运数字:[99,3,0,7,100]进程结束,退出代码为0:

我将打印所有内容,因为对于每个循环,它将输入一个条件。.第一个循环将打印1,第二个将打印2,依此类推..请提供所需输出样本的详细信息

I was able to fix your program and now it works great. 我能够修复您的程序,现在效果很好。 It looks like you were close, but you need to give luckyInt a value and then compare that value to the index in a loop; 看起来您已经接近了,但是您需要给luckyInt一个值,然后将该值与循环中的索引进行比较; rather than using luckInt as the index. 而不是使用luckInt作为索引。 I added a random number generator: 我添加了一个随机数生成器:

import random


def random_num(hi):
    lo = 0
    hi = hi-1
    return random.randint(lo, hi)


f_list = ['nothing', 'You will get an A', 'You will learn to program', 'You are 
destined to become a master coder \n',
'May the force be with you', 'Wow. You look like a programmer', 'The code is 
strong with this one \n',
'You will be testing your code often', 'Taco cat spelled backwards is taco cat \n',
'Your name will go down, down, down in history', 'You will master python']

luckyInt = random_num(len(f_list))

for i in range(len(f_list)):
    if luckyInt == i:
        print(f_list[i])

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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