This week's book giveaway is in the Agile and Other Processes forum. We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line! See this thread for details.
I'm new to python and my teacher asked to code a program that:
Write a program that implements the “Spin-to-Win” contest. There are three contestants (1, 2, and 3) who watch the
wheel spin and hope to win some money. An initial random number from 1-10 is generated to determine which
contestant or the house wins (or loses) money: a value from 1-3 means contestant #1, #2 or #3 wins/loses money; a
value from 4-9 means the house wins/loses money. A value of 10 means the game is over. After the contestant/house
is selected, the wheel is spun (the “wheel” is actually a random number generator). The “wheel” has 100 different
possibilities, and they are processed as follows
A number in the range 1-25 awards $500 to the appropriate contestant or house
A number in the range 26 -50 awards $1000 to the appropriate contestant or house
A number in the range 51-75 bankrupts the appropriate contestant or house
A number in the range 76-100 awards $1500 to the appropriate contestant or house
The game repeats until a contestant number of 10 is drawn.
I'm trying to get the variable addMoney to assign to another value if the conditions meet ex: if the random number rolls a 1 it contestant one's money. also it has to displays all the text.
The code and the sample output attachment you have posted do not match...
This code will always print all zeros because c1, c2, c3, and house are never added to.
The reason your program is not working as you would expect can be summarized in these few lines:
In particular writing addMoney = c1 does not mean that addMoney and c1 represent the same piece of memory. Assigning addMoney, respectively c1, aftwards will not change the value of c1, respectively addMoney.
One solution would be to assign both winner and addMoney first and then uswe nested if statements. I mean something like this:
But this quickly becomes tedious to write. A better solution would be to use a list to keep track of how much money everybody has. The code would start looking a bit like this:
You will find plenty of informations about how to use lists if you google for "python lists tutorial".
A few more comments:
import math is not used - this line could be removed
the name variable and consequenty c1, c2, c3, house1 are never used - they could be removed
winner is not a good variable name (because it does not containt the winner, just who is next), something like nextPick would be better
Hope this helps.
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you: