Python dice game

I must create a dice game that generates numbers from 1 to 6. It will then throw the dice 50 times and it will count the number of odd numbers and even numbers. I'm using Python.

Here is my code:

import random

# Determine odd and even numbers

throws = 0
even = 0
odd = 0
maxthrows = 50

print "Even : Odd"

while True:
    throws += 1
    if throws == maxthrows:
        break

dice = random.randrange(6)

if dice % 2 == 1:
    odd += 1
else:
    even += 1
print even, " : ", odd

raw_input("Press enter to exit.")

Answers


Your loop is wrong, it should be:

while throws != maxthrows:
    throws += 1
    dice = random.randrange(6)
    if dice % 2 == 1:
        odd += 1
    else:
        even += 1

Notice that:

  • Whenever possible, the exit condition should be used in the loop condition, not in an if ... break
  • The part where you ask if the dice is odd must be inside the loop, in Python indentation matters - a lot!

Need Your Help

How to create PayPal Payflow Pro test account

paypal

I need a PayPal Payflow Pro test account in order to create and test/use a WordPress plugin for payment with this method. I've done something similar for PayPal Website Payments Pro product and hav...

SFML: Linker Errors on OS X

c++ linker sfml

This may seem like quite a 'noob' question: I've downloaded SFML-1.6 for Mac and I have placed the frameworks into my /Library/Frameworks folder. After trying to compile an example SFML application...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.