-1

So i have this program and it works (mathematically) even though the user can use it without problem. The only matter is, in the end when the program asks the user to quit or to restart it dosen't ! So how can i restart the game !!! Thank you all for your help

print('guess what number im thinking of\n')
Max=int(input('Give us your hights number that your interval can have:')) 
Min=0
print'Now think to a number between 0 and',Max,'once you have chose your numebr'
print'Great !'
print'its your turn to help us,if the number is :Exacte (0),higher(1)or lower(-1)'
milieu=(Max-Min)/2
print int(milieu),'is that the number your thinking of ?'
z=input(str('its important that you only answer by 0, 1 or -1:'))
x=False 
while milieu<Max and x==False: 
    if z==0:                  
        print('We had guess what number you chosed')
        x=True
    while z!=0:   
        if z==1: 
            x=False
            Min=milieu  
            milieu=milieu+((Max-Min)/2)   
            print(int(milieu))
            z=input('its important that you only answer by E, G or P:') 
        if z==-1:
            x=False
            Max=milieu  
            milieu=(milieu-((Max-Min)/2))
            print(int(milieu))
            z=input('its important that you only answer by E, G or P:')
            break
while x==True:
    a=input('[5] to go out game\nor [9] to restart:')
    if a==5: break 
    print ("restart")
    if a==9: 
        x=False
toblerone_country
  • 577
  • 14
  • 26

2 Answers2

2

1) You are getting a string from the input function, you should cast it to an int.

2) You should use a function to achieve your objective

def game():
   ... # all the code for your game that you posted

if __name__ == "__main__":
   while True:
     game()
     response = int(raw_input('[5] to go out game\nor [9] to restart:'))
     if response != 9:
       break
lc2817
  • 3,722
  • 16
  • 40
-1

It looks like your main program is out of the while loop. You should put your code in a function and call that in the main loop. Also, in your main loop, you are testing int and strings. Remember that "5" != 5. You can eighter convert it to an int directly, or just test it as "5" instead of 5. I prefer to just test it as 5 so if the user enter non-numerical digits, it will not creash your program.

def game():
    print('guess what number im thinking of\n')
    Max=int(input('Give us your hights number that your interval can have:')) 
    Min=0
    print'Now think to a number between 0 and',Max,'once you have chose your numebr'
    print'Great !'
    print'its your turn to help us,if the number is :Exacte (0),higher(1)or lower(-1)'
    milieu=(Max-Min)/2
    print int(milieu),'is that the number your thinking of ?'
    z=input(str('its important that you only answer by 0, 1 or -1:'))
    x=False 
    while milieu<Max and x==False: 
        if z==0:                  
            print('We had guess what number you chosed')
            x=True
        while z!=0:   
            if z==1: 
                x=False
                Min=milieu  
                milieu=milieu+((Max-Min)/2)   
                print(int(milieu))
                z=input('its important that you only answer by E, G or P:') 
            if z==-1:
                x=False
                Max=milieu  
                milieu=(milieu-((Max-Min)/2))
                print(int(milieu))
                z=input('its important that you only answer by E, G or P:')
                break

while True:
    a=input('[5] to go out game\nor [9] to restart:')
    if a=="5": 
        break 
    if a=="9":
        print("restart")
        game()
wcb98
  • 172
  • 1
  • 1
  • 10
  • I think the diffs are very easy to find. He just packed the first loop into a function and changed to second loop to call that function. – Grochni Sep 29 '14 at 00:19
  • so if i get it... i should do it like what @wcb98 said. the only difference is too but the while true inside the function – Ali Assafiri Sep 29 '14 at 00:23
  • yes, your main game is inside the game() function, which makes the code easier to read – wcb98 Sep 29 '14 at 00:57