【问题标题】:Giving out 1000 answers at the same time in while loop在 while 循环中同时给出 1000 个答案
【发布时间】:2018-11-07 20:48:37
【问题描述】:

(我已经开始讨厌while在我的学校工作......)

我现在的问题是,我需要创建一个程序,向用户询问篮球投掷准确度的百分比。并模拟 1000 次投掷 - 对于每个答案它给出的命中或未命中结果。

如果用户输入 45 作为百分比的程序工作示例

throwing_accuracy = int(input("Enter throwing percentage")): 45

#Somewhere here would be while :
1. Throw hit
2. Throw hit
3. Throw hit
5. Throw missed
#.........#
999. Throw hit
1000. Throw hit
   #And then prints also a result how many hits there were
There were 458 hits

程序必须使用while 构建,我阅读了一些可以使用ifelse 语句的提示,并且应该使用from random import randint,因为有可能使用一行if randint(1,100) <= percentage

【问题讨论】:

  • 你试过什么?您具体需要哪些帮助?
  • @Carcigenicate - 我只知道程序应该如何工作,但不知道如何构建它......
  • 这对于这里来说太宽泛了。该站点旨在帮助您处理现有的损坏代码。如果您需要一般帮助,Reddit 的 r/learnprogramming 可能是一个很好的查看方式。但请注意,您访问的大多数地方都会要求您指定您需要帮助的具体内容。 “我如何开始这个”是相当广泛的,即使在具有广泛关注的网站上也是如此。
  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 On topichow to ask... the perfect question 在此处申请。 StackOverflow 不是设计、编码、研究或教程资源。但是,如果您遵循您在网上找到的任何资源,进行诚实的编码尝试并遇到问题,那么您将有一个很好的示例可以发布。
  • 你离题不远了,看看你的其他帖子,仔细阅读解决方案,你最终会明白的

标签: python python-3.x


【解决方案1】:

你可以编写这样的程序:

import random
percentage_accuracy = int(input("Input throwing accuracy here: "))
hits = 0
throws = 1
while throws <= 1000:
    if random.randint(1, 100) <= percentage_accuracy:
        hits += 1
        print("{}: Hit".format(x))
    else:
        print("{}: Miss".format(x))
    throws += 1
print("")
print("Player results:")
print("{} Hits".format(hits))
print("{} Misses".format(1000-hits))

使用 for 循环会更有效,例如:

for throws in range(1,1001):

而不是:

throws = 1
while throws <= 1000:
    throws += 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多