基本的硬币鳍状肢程序 - 逻辑错误帮助 [英] Basic coin flipper program - logical error help

查看:68
本文介绍了基本的硬币鳍状肢程序 - 逻辑错误帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习Python。我已经创建了一个简单的硬币翻转程序 -

这里是代码:


[来源]

#Coin flipper

随机进口


头= 0

尾巴= 0

counter = 0

coin = random.randrange(2)


while(counter< 100):

if(coin == 0):

头+ = 1

柜台+ = 1

否则:

尾巴+ = 1

计数器+ = 1


coin = random.randrange(2)

print" \ n硬币落在头上 ,头部,时间。

打印\ n硬币落在尾巴上,尾巴,时间。

[/ source]


<<<我确定[来源]标签不起作用 - 我在那里通过它们

无论如何。> >>


程序运行 - 但是 - 它会给我100个头或100个尾巴。

有人能发现逻辑错误吗?

谢谢


~Dan

解决方案

在星期二,2006-02-21 16:14 -0800,DannyB写道:< blockquote class =post_quotes>我只是在学习Python。我创造了一个简单的硬币鳍状肢程序 -
这里是代码:

[来源]
#Coin鳍状肢
随机导入

head = 0
tails = 0
counter = 0

coin = random.randrange(2)

while(counter< 100) :
if(coin == 0):
head + = 1
counter + = 1
else:
tails + = 1
counter + = 1

coin = random.randrange(2)

打印\ n硬币降落在头上,头,时代。
打印\ n硬币落在尾巴上,尾巴,时代。
[/ source]

<<<<我确定[来源]标签不起作用 - 无论如何我在那里通过
。>>>

程序运行 - 但是 - 它会给我100个头或100个尾巴。 />有人能发现逻辑错误吗?




是的。在while循环中放置coin = random.randrange(2)。


随机导入


heads = 0

tails = 0

counter = 0

coin = random.randrange(2)


while(counter < 100):

if(coin == 0):

head + = 1

counter + = 1

否则:

尾巴+ = 1

柜台+ = 1


coin = random.randrange(2)


打印\ n硬币降落在头上,头,时间。

打印\ n硬币降落在尾巴上 ,尾巴,时间。

-

此邮件已通过MailScanner扫描病毒和

危险内容,并且

被认为是干净的。


DannyB写道:

我只是在学习Python 。我创造了一个简单的硬币鳍状肢程序 -
这里是代码:

[来源]
#Coin鳍状肢
随机导入

head = 0
tails = 0
counter = 0
while(counter< 100):


coin = random .randrange(2)


Claudio

if(coin == 0):
head + = 1
counter + = 1
其他:
尾巴+ = 1
计数器+ = 1

币= random.randrange(2)

打印" \\ \\ n硬币落在头上,头,时间。
打印\ n硬币落在尾巴上,尾巴,时代。
[/ source]

<<<我确定[source]标签不起作用 - 无论如何我在那里通过它们。>>>
<程序运行 - 然而 - 它会给我100个头或100个尾巴。
有人能发现逻辑错误吗?

谢谢

~Dan



DannyB写道:

我只是在学习Python。我创造了一个简单的硬币鳍状肢程序 -
这里是代码:

[来源]
#Coin鳍状肢
随机导入

head = 0
tails = 0
counter = 0

coin = random.randrange(2)

while(counter< 100) :
if(coin == 0):
head + = 1
counter + = 1
else:
tails + = 1
counter + = 1

coin = random.randrange(2)

打印\ n硬币降落在头上,头,时代。
打印\ n硬币落在尾巴上,尾巴,时代。
[/ source]

<<<<我确定[来源]标签不起作用 - 无论如何我在那里通过
。>>>

程序运行 - 但是 - 它会给我100个头或100个尾巴。有人能发现逻辑错误吗?

谢谢

〜丹



看起来很像你的作业,但是我'克给你一个线索。

你需要把你的硬币扔进你的循环中。否则

你只需抛硬币一次,然后循环100次,使用

相同的值。


-Larry Bates

I''m just learning Python. I''ve created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)
print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I''m sure the [source] tags don''t work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

Thanks

~Dan

解决方案

On Tue, 2006-02-21 at 16:14 -0800, DannyB wrote:

I''m just learning Python. I''ve created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)
print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I''m sure the [source] tags don''t work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?



Yes. Put coin = random.randrange(2) inside the while loop.

import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)

print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


DannyB wrote:

I''m just learning Python. I''ve created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

while (counter < 100):
coin = random.randrange(2)

Claudio
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)
print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I''m sure the [source] tags don''t work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

Thanks

~Dan



DannyB wrote:

I''m just learning Python. I''ve created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)
print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I''m sure the [source] tags don''t work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

Thanks

~Dan


Looks an awful lot like your homework, but I''ll give you a clue.
You need to get the your coin tosses inside your loop. Otherwise
you only toss the coin once and then loop 100 times with the
same value.

-Larry Bates


这篇关于基本的硬币鳍状肢程序 - 逻辑错误帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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