简体   繁体   English

Python程序不给出输出

[英]Python program not giving output

I have been trying to write out a problem birthday.py.我一直在尝试写出一个问题birthday.py。 Suppose that people enter an empty room until;假设人们进入一个空房间,直到; a pair of people share a birthday.一对人共享一个生日。 On average, how many people will have to enter before there is a match?平均而言,在比赛开始前需要多少人入场? Write a program birthday.py that accepts trials (int) as command-line argument, runs trials experiments to estimate this quantity — each experiment involves sampling individuals until a pair of them share a birthday, and writes the value to standard output.编写一个接受试验(int)作为命令行参数的程序birthday.py,运行试验实验来估计这个数量——每个实验都涉及对个体进行采样,直到他们有一对共享生日,然后将值写入标准输出。 My trouble has been getting an output.我的麻烦是获得输出。 If I enter 1000 in the command terminal 24 should be the output but it doesn't give anything.如果我在命令终端中输入 1000 应该是输出,但它什么也没给出。

DAYS_PER_YEAR = 365
    
    # Accept trials (int) as command-line argument.
    trials = int(sys.argv[1])
    
    # Set count, denoting the total number of individuals sampled across the trials number of
    # experiments, to 0.
    total = 0
    
    for i in range(trials):
        # Perform trials number of experiments, where each experiment involves sampling individuals
        # until a pair of them share a birthday...
    
        # Setup a 1D list birthdaysSeen of DAYS_PER_YEAR Booleans, all set to False by default. This
        # list will keep track of the birthdays encountered in this experiment.
        birthdaysSeen = stdarray.create1D(DAYS_PER_YEAR, False)
        while True:
            # Sample individuals until match
            n = stdrandom.uniformInt(0, 364)
            # Increment count by 1.
            total += 1
    
            # Set birthday to a random integer from [0, DAYS_PER_YEAR).
            birthday = stdrandom.uniformInt(0, DAYS_PER_YEAR)
    
            if birthdaysSeen[birthday]:
                # If birthday has been encountered, abort this experiment, ie, break.
                break
        else:
            # Record the fact that we are seeing this birthday for the first time.
            birthdaysSeen[birthday] = True
    # Write to standard output the average number of people that must be sampled before a match,
    # as an int.
    stdio.writeln(count // trials)

in Python, you can do在 Python 中,你可以做

print(count // trials)

for almost any data structure as nearly all of them have a __repr__() or an __str()__ method defined.对于几乎所有数据结构,几乎所有数据结构都定义了__repr__()__str()__方法。

And since count and trials should be pure numerical data types, print() should do the trick由于counttrials应该是纯数字数据类型,所以print()应该可以解决问题

Your while loop has an else, which turns it into an infinite loop.你的 while 循环有一个 else,它把它变成了一个无限循环。 Tab this line in to match up with the if instead of the while :将这一行插入以匹配if而不是while

    else:
        # Record the fact that we are seeing this birthday for the first time.
        birthdaysSeen[birthday] = True

Also, your print at the end should be total // trials not count // trials此外,你最后的打印应该是total // trialscount // trials

我建议使用print()而不是stdio.writeln()

stdio.writeln is not a built in Python function. stdio.writeln不是内置的 Python 函数。 Use print or import stdout from the sys module and write to it like this:从 sys 模块使用print或 import stdout 并像这样写入:

from sys import stdout

stdout.write(count // trials + "\n")

The \\n is a newline. \\n是换行符。 print automatically appends a newline. print自动附加换行符。 sys.stdout.write does not. sys.stdout.write没有。

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

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