需要python的帮助,简单(关于轮流) [英] Needed help on python, easy (about taking turns)

查看:62
本文介绍了需要python的帮助,简单(关于轮流)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种共享算法中,探矿者轮流获取金块。编写一个名为turns(n)的函数来计算每个探测器获得的金量,并在Turtle Graphics中显示。你的函数应该看起来像这个伪代码版本。该函数应该有一个参数,即探矿者之间共享的金块数量。我希望你会想要编写轮流调用的其他函数(移动乌龟,绘制正方形等)。


将p1'的黄金设为0

将p2'的黄金设为0

给定数量的金块

将金块设置为10到100之间的随机数(其值)

如果是prospetor1轮到

将金块添加到p1

显示prospector1'的金块

else

将金块添加到p2

显示prospector2'的金块



这是我的所得,但我不知道如何轮流并显示每个玩家的结果

展开 | 选择 | Wrap | 行号

解决方案

展开 | 选择 | Wrap | 行号

我不太明白。但我想你是说你依次向p1和p2添加一个随机数,并依次从p1和p2延伸一条线,所以你可以看到它们在某种程度上相互竞争。


与所有事情一样,有很多方法可以做到这一点。但由于这是家庭作业,你的主要问题似乎是轮流,我会帮助这方面。基本上我们需要经历n个掘金,将它们分散在两个探矿者之间。我将使用%运算符拆分代码,它基本上返回余数(例如10%3等于1)

展开 | 选择 | Wrap | 行号

如何绘制乌龟功能

由P1和P2的值决定?


In this sharing algorithm the prospectors take it in turns to get gold nuggets. Write a function called turns(n) to calculate the amount of gold that each prospector gets, and to display this in Turtle Graphics. Your function should look something like this pseudocode version. The function should have a single parameter, the number of gold nuggets to be shared between the prospectors. I expect that you will want to write other functions that are called by turns (to move the turtle, draw squares and so on).

set p1''s gold to 0
set p2''s gold to 0
for a given number of nuggets
set nugget to a random number between 10 and 100 (its value)
if it is prospetor1''s turn
add nugget to p1
display prospector1''s nugget
else
add nugget to p2
display prospector2''s nugget



Here is what i have got, but i do not know how to take turns and show the result of each player

Expand|Select|Wrap|Line Numbers

解决方案

Expand|Select|Wrap|Line Numbers


I don''t quite understand. But I think you''re saying that you add a random number to p1 and p2 in turn, and also extend a line from p1 and p2 in turn, so you can see them race against each other in a way.

As with all things there are many ways of doing this. But since this is homework and your main problem seems to be with taking the turns, I''ll help with that aspect. Basically we need to go through n nuggets, splitting them between the two prospectors. I''ll split the code using the % operator, which essentially returns the remainder (e.g. 10%3 is equal to 1)

Expand|Select|Wrap|Line Numbers


how about how to draw the turtle function
determine by the value of P1 and P2?


这篇关于需要python的帮助,简单(关于轮流)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆