谢谢 ~Dan 解决方案 在星期二,2006-02-21 16:14 -0800,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个尾巴。 />有人能发现逻辑错误吗? 是的。在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]标签不起作用 - 无论如何我在那里通过它们。>>> 有人能发现逻辑错误吗? 谢谢 ~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 flipperimport random heads = 0tails = 0counter = 0 coin = random.randrange(2) while (counter < 100):if (coin == 0):heads += 1counter += 1else:tails += 1counter += 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 thereanyway.>>> 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 = 0tails = 0counter = 0 coin = random.randrange(2) while (counter < 100):if (coin == 0):heads += 1counter += 1else:tails += 1counter += 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 anddangerous content by MailScanner, and isbelieved 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 ~DanDannyB 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 ~DanLooks an awful lot like your homework, but I''ll give you a clue.You need to get the your coin tosses inside your loop. Otherwiseyou only toss the coin once and then loop 100 times with thesame value. -Larry Bates 这篇关于基本的硬币鳍状肢程序 - 逻辑错误帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 21:10