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.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Picking a contestant for a game then adding money to their name based on if statements

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



Code:
import random
import math

c1= 0
c1n= "1"
c2= 0
c2n= "2"
c3= 0
c3n= "3"
house= 0
house1= "House"
addMoney= 0
winner= 0

#Which person to add money to


#addMoney = person choosen to be given the money

while winner != 10:
   winner= random.randint(1,10)
   
   if winner == 1:
       addMoney = c1
       name= c1n
   elif winner == 2:
       addMoney = c2
       name= c2n
   elif winner == 3:
       addMoney = c3
       name= c3n
   elif winner == 4 or 5 or 6 or 7 or 8 or 9:
       addMoney = house
       name= house
   
   moneyPick= random.randint(1,100)

   if moneyPick >= 100:
       addMoney= addMoney+1500
       print("....Wheel Spin: ",moneyPick)
       print("won $1500")
   elif moneyPick >= 76:
       addmoney=addMoney
       print("....Wheel Spin: ",moneyPick)
       print(" hit bankrupt")
   elif moneyPick >= 51:
       addMoney= addMoney+1000
       print("....Wheel Spin: ",moneyPick)
       print(" won $1000")
   elif moneyPick >= 26:
       addMoney= addMoney+500
       print("....Wheel Spin: ",moneyPick)
       print(" won $500")

#print("Contestant",var,"....Wheel Spin: ",moneyPick)


print("*********************")
print("     Final Total     ")
print("*********************")
print("Contestant #1: ",c1,)
print("Contestant #2: ",c2,)
print("Contestant #3: ",c3,)
print("        House: ",house,)
Annotation-2020-03-11-214536.png
[Thumbnail for Annotation-2020-03-11-214536.png]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nathaniel,

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:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!