[英]Python RNG and counter
I'm having trouble on a homework assignment that is asking me to do as follows.我在要求我做如下作业的家庭作业上遇到了麻烦。 Write a code that conduct following actions:
编写执行以下操作的代码:
Define a function that returns an integer between 1 and 10.定义一个 function,返回 1 到 10 之间的 integer。
Call the defined function to get the random integer.调用定义的function得到随机的integer。
Continue drawing a random integer until you get a number that is larger than or equal to 8.继续随机抽取 integer,直到得到一个大于或等于 8 的数字。
Display how many draws did it take before you stopped显示在您停止之前进行了多少次抽奖
You should import random module.您应该导入随机模块。
I welcome the completion of this assignment but would be very grateful for an explanation as well.我欢迎完成这项任务,但也非常感谢您的解释。 I'm currently at a loss of how to keep track of how many draws it took before I stopped.
我目前不知道如何跟踪我停下来之前抽了多少次。 What I have so far is as follows, please help?
目前我的情况如下,请帮忙?
from random import randint
rng= randint(1,11)
def random_number_generator():
print(rng)
while rng<=7:
break
random_number_generator()
from random import randint
rng = randint(1, 11) # get a random number between 1 and 11
def random_number_generator(): # define a function
print(rng) # print random generated number
while rng <= 7: # if rng <= 7 condition is True
break # break out of while loop immediately
# no return statement, so random_number_generator returns none
random_number_generator() # Call function
Going by the code you provided, lets look at the 4 tasks you need to solve.根据您提供的代码,让我们看看您需要解决的 4 个任务。
- Define a function that returns an integer between 1 and 10.
定义一个 function,返回 1 到 10 之间的 integer。
As you already know, you can define a function with the keyword def
.如您所知,您可以使用关键字
def
定义 function。 So your function header def random_number_generator()
defines a function with the name random_number_generator
that takes no arguments when the function is called, hence the empty parenthesis ()
.因此,您的 function header
def random_number_generator()
定义了一个名为random_number_generator
的 function,在调用 function 时不使用 arguments,因此空括号()
。
In Python every function returns None
per default.在 Python 中,每 function 默认返回
None
。 If you want to change this behaviour, you have to add a return
statement to your function.如果你想改变这种行为,你必须在你的 function 中添加一个
return
语句。
def get_two():
return 2
x = get_two() # store return value in variable x
print(x) # prints 2
get_two
returns the number 2
when called. get_two
在调用时返回数字2
。
- Call the defined function to get the random integer.
调用定义的function得到随机的integer。
You correctly imported randint
from the random
module to generate a random integer between 1
and 10
.您从
random
模块正确导入了randint
以生成一个介于1
和10
之间的随机 integer。 But you have to be careful with your choice of numbers as randint
has both values inclusive.但是您必须小心选择数字,因为
randint
包含这两个值。 That is randint(1, 11)
will give you a random integer between 1 and 11.那就是
randint(1, 11)
会给你一个随机的 integer 在 1 和 11 之间。
- Continue drawing a random integer until you get a number that is larger than or equal to 8.
继续随机抽取 integer,直到得到一个大于或等于 8 的数字。
Continue drawing in this context means to call the random_number_generator
function over and over again until you get a number that is larger than or equal to 8 . Continue drawing在这个上下文中意味着一遍又一遍地调用
random_number_generator
function 直到你得到一个大于或等于 8的数字。
Your idea of the while
loop with the given condition was correct.您对具有给定条件的
while
循环的想法是正确的。 But think about the placement.但是考虑一下位置。 Does it make sense to have the loop inside a function that only generates 1 random number per call?
在每次调用仅生成 1 个随机数的 function 中设置循环是否有意义?
- Display how many draws did it take before you stopped
显示在您停止之前进行了多少次抽奖
Use the print
function to display how many draws you did.使用
print
function 显示您抽了多少次。 In order to get the number of draws you can add a counter
variable, that gets incremented with each loop.为了获得绘制次数,您可以添加一个
counter
变量,该变量会随着每个循环而递增。 With each loop you add 1 to your counter
variable.在每个循环中,您将 1 添加到
counter
变量。 There are 2 options for you to do this:有 2 个选项供您执行此操作:
counter = counter + 1
which can be a handful counter = counter + 1
这可能是少数counter += 1
which is the same as 1. but you don't need to write counter
twice.counter += 1
的简写,它与 1 相同。但您不需要写counter
两次。 With this you can built a simple counter just like this:有了它,您可以像这样构建一个简单的计数器:
counter = 0
i = 10
while i > 3:
counter += 1 # Increment counter by 1 each loop
i -= 2 # Decrement i by 2 each loop
print(counter) # prints 4
With that being said you can now start to implement the given tasks.话虽如此,您现在可以开始实施给定的任务。
randint
from random
modulerandom
模块导入randint
1
and `10``1
到 10 之间的随机数from random import randint # 1.
def random_number_generator(): # 2.
return randint(1, 10)
counter = 0 # Initialize counter with 0
while random_number_generator() < 8: # Draw a number until number is >= 8
counter += 1 # increment counter to count draws
print(counter, "draw(s) needed until a number greater than or equal 8 was drawn") # Display draws needed
As you don't need to store the randomly generated number, you can use the function call of random_number_generator
in your loop condition to check if the number is greater than or equal 8.由于不需要存储随机生成的数字,因此可以在循环条件中使用
random_number_generator
的 function 调用来检查数字是否大于或等于 8。
Inside the loop you'll just count how many times you need to draw a number.在循环中,您只需计算需要绘制数字的次数。
But you are free to store the value in a variable rng
to check against the loop condition.但是您可以自由地将值存储在变量
rng
中以检查循环条件。 In this case you need to assign random_number_generator()
twice to rng
.在这种情况下,您需要将
random_number_generator()
分配给rng
两次。 One time outside the while loop to initialize the variable rng
for the loop condition and a second time inside the loop to change the value rng
is holding.第一次在 while 循环之外为循环条件初始化变量
rng
,第二次在循环内更改rng
持有的值。
See comments in code below.请参阅下面代码中的注释。
'''
Generate random integers until a value equal or greater than 8 is returned.
Report how many attempts it took.
'''
from random import randint
# Define a function which ONLY returns a random integer between 1 and 10
def rand_int():
return (randint(1,10)) # parameters are inclusive, i.e. can return both 1 and 10
# Set initial values for a counter and the random integer result
counter = 0
random = 0
while random <= 7: # Continue running while random int is too low
counter += 1 #Increment counter variable by 1 each loop
random = rand_int()
print('Current count is ', counter, ', integer is ', random)
if counter > 100:
print('Too many tries, something has probably gone wrong')
break
# Display success message
print('Success! Integer ', random, ' returned after ', counter, 'attempts!')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.