From the following code, how can I enter a for and a while? (trinket) [closed]

1
for i in range(2):
  print ("Hello")

import random

def game():
  x = int(raw_input("Give me a number"))
  z = random.randint(1,10)
  if x==z:
    print "you won"
  else:
    print "I´am sorry the winning number is", z
    return;

def result():
  while game()<11:
    print "that statement is true"
    return;

game()
result()  
    
asked by EngelArt 18.05.2017 в 21:00
source

1 answer

1

Let's analyze your code and see the problems:

There is a missing for for up .. print hello twice ..

then define two functions:

game()
result()

and then you make a call to game()

game() returns .. nothing .... internally game() asks for a number, compares it with the calculated number, warns if you won or not, and returns ...

then result() does not do more than bundled, because it calls back game() and sets if what is returned (which is nothing, because the return in game is empty) is less than 11. and that's why gives true ....

Now ... let's think .. if what you want is to repeat game() until you win .. you should not run game() until you actually win ?? and how would we do that ... maybe if game() would return us if you won or not ... and that would be executed until it was true ...

    
answered by 18.05.2017 в 21:27