0

The point of this game is to have two people flip a coin, and if the first person gets heads and the second person gets heads, the first person wins, but if the second person gets the opposite coin they win. My code's output just displays "True" a thousand times, but I have a for loop in my method that isn't working?

import numpy as np

class Students():

  def __init__(self,flip,history):

    self.flip=flip

    self.history=history

  def flipcoin(self):

    for self.flip in range (0,1000):

      self.flip= np.random.random()

      if (self.flip<0.5):

        self.flip=0

      else:

       self.flip=1

       print (str(self.flip))

      self.history= self.flip

      print(self.history)

      return (str(self.flip))

student1=Students(flip=0,history=[])

student1.flipcoin()

student2=Students(flip=0,history=[])

student2.flipcoin()

for Students in range (0,1000):

  if (student1==student2):

       print('False')

  else:

       print('True')

print(student1.flip,student1.history)
Warcupine
  • 4,460
  • 3
  • 15
  • 24
  • 1
    `return` exits the loop and the function. – Fred Larson Feb 17 '22 at 18:31
  • 1
    Does this answer your question? [How to use a return statement in a for loop?](https://stackoverflow.com/questions/44564414/how-to-use-a-return-statement-in-a-for-loop) – Fred Larson Feb 17 '22 at 18:32
  • I read the article and I found the information on returns useful, but for some reason even after I remove it the method only goes through once and just displays a thousand 'true' statements – Artemis Feb 17 '22 at 18:38

1 Answers1

0

So to answer your immediate question, your first problem is here:

for Students in range(0, 1000):
    if student1 == student2:
        print('False')
    else:
        print('True')

What you are comparing here are two instances of "Students". Since those are different instances, they are not equal. (And I'm not sure if you have your print statements reversed -- when that comparison returns False, the code prints 'True'.)

Chris Curvey
  • 9,738
  • 10
  • 48
  • 70
  • Ohhhh okay that makes more sense. But in order to print each instance , would I put the boolean inside another method or is there a different way to make those two functions equal to one another? – Artemis Feb 17 '22 at 19:24
  • wait, let's back up. What question are you trying to answer? I **think* the question would be "How many times does student1 win?" – Chris Curvey Feb 17 '22 at 22:16