简体   繁体   中英

printing str values from list loop that resets if duplicates found in the result

I really can't figure out what's wrong with this loop, I tried editing it several times, but sometimes it prints duplicates ignoring the condition. after I edited it, it stopped printing anything at all... can somebody whos more experienced find out whats wrong here...

first attempt: it prints results with duplicates

from ast import Break
import random
import secrets

def checkIfDuplicates_2(x):
     x.append(random.choice(questions))
     x.append(random.choice(questions))
     x.append(random.choice(questions))
     x.append(random.choice(questions))
     x.append(random.choice(questions))
     for elem in x:
        if elem in x:
            x = []
            x.append(random.choice(questions))
            x.append(random.choice(questions))
            x.append(random.choice(questions))
            x.append(random.choice(questions))
            x.append(random.choice(questions))
        elif elem not in x:
            print (x[0])
            print (x[1])
            print (x[2])
            print (x[3])
            print (x[4])
            break

questions = ["- Current Ratio:", "- Quick Ratio(ACID):", "- Inventory Turnover:", "- Average Age of Inventory:", "- Average Collection Period(ACP):", "- Average Payment Period(APP):", "- Total Assets Turnover:", "- Debit Ratio:", "- Times-Interest Earned Ratio(TIE):", "- Fixed-Payment Coverage Ratio(FPCR):", "- Gross Profit Margin(GPM):", "- Operating Profits Margin(OPM):", "- Net Profit Margin(NPM):", "- Earnings Per Share(EPS):", "- Return on Total Assets(ROA):", "- Return on Common Equity(ROE):", "- Price/Earnings Ratio(P/E):", "- Market/Book Ratio(M/B):"]
pool = []

# space between prints
spaces = " "
print(spaces * 5)
print("Find the following ratios:")
checkIfDuplicates_2(pool)

second attempt: I thought the problem with the 2nd (if) it has "x = []" so it clears the list so the loop can have a fresh start, so I replaced it with x.clear(). now it doesn't print anything at all

from ast import Break
import random
import secrets

def checkIfDuplicates_2(x):
     x.append(random.choice(questions))
     x.append(random.choice(questions))
     x.append(random.choice(questions))
     x.append(random.choice(questions))
     x.append(random.choice(questions))
     for elem in x:
        if elem in x:
            x.clear()
            x.append(random.choice(questions))
            x.append(random.choice(questions))
            x.append(random.choice(questions))
            x.append(random.choice(questions))
            x.append(random.choice(questions))
        elif elem not in x:
            print (x[0])
            print (x[1])
            print (x[2])
            print (x[3])
            print (x[4])
            break

questions = ["- Current Ratio:", "- Quick Ratio(ACID):", "- Inventory Turnover:", "- Average Age of Inventory:", "- Average Collection Period(ACP):", "- Average Payment Period(APP):", "- Total Assets Turnover:", "- Debit Ratio:", "- Times-Interest Earned Ratio(TIE):", "- Fixed-Payment Coverage Ratio(FPCR):", "- Gross Profit Margin(GPM):", "- Operating Profits Margin(OPM):", "- Net Profit Margin(NPM):", "- Earnings Per Share(EPS):", "- Return on Total Assets(ROA):", "- Return on Common Equity(ROE):", "- Price/Earnings Ratio(P/E):", "- Market/Book Ratio(M/B):"]
pool = []

# space between prints
spaces = " "
print(spaces * 5)
print("Find the following ratios:")
checkIfDuplicates_2(pool)

Have you considered the closely related random.sample() method? Takes a list and randomly samples the second argument's number of elements from the list.

total_questions = 5
pool = random.sample(questions, total_questions)
print(pool)

You need to check if the condition is satisfied before print it. If not, try again.

def checkIfDuplicates_2(x):
    total_questions = 5

    while True:
        for i in range(total_questions):
            x.append(random.choice(questions))

        if len(set(x)) == len(x):
            for i in range(len(x)):
                print(x[i])

            break
        else: 
            x = []               

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