Make this code better

Help!! :)

I need someone to please give me some advice and help me fix my code! I'm new to coding and any advice would be super helpful! The code is supposed to have the computer guess what number that the user is thinking of using input (c, h, l,). Pls help!

MaddieW.py
/** start = 0 mid = 50 stop = 100 print("Please think of a number between 0 and 100!") print("Is your secret number" + " " + str(mid) + "?") x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ") while x != 'c': if x == 'h': stop = mid mid = int((mid + start)/2) elif x == 'l': start = mid mid = int((mid + stop)/2) print("Is your secret number" + " " + str(mid)) else: print("Sorry, I did not understand your input.") print("Is your secret number" + " " + str(mid)) input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ") if x == 'c': print("Game over. Your secret number was:" + " " + str(mid)) * Any language, just go for it. */

Reactions 4 reactions

Pascalos Pascal Anema contributed to this code
1
I made it better for you, I don't know Python, only php and java, but I don't think this causes any errors! :)
MaddieW.py
@@ -1,29 +1,30 @@
 /**
-start = 0
-mid = 50
-stop = 100
+from random import randint
+low = 0
+guess = randint(low, high)
+high = 100
 
 
 print("Please think of a number between 0 and 100!")
-print("Is your secret number" + " " + str(mid) + "?") 
+print("Is your secret number " + str(guess) + "?") 
 x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
 
 while x != 'c':
     if x == 'h':
-        stop = mid
-        mid = int((mid + start)/2)
+        high = guess
+        guess = randInt(start, stop)
     elif x == 'l':
-        start = mid
-        mid = int((mid + stop)/2)
-        print("Is your secret number" + " " + str(mid)) 
+        low = guess
+        guess = randInt(low, high)
+        print("Is your secret number " + str(guess) + "?") 
     else:
          print("Sorry, I did not understand your input.")  
-         print("Is your secret number" + " " + str(mid)) 
+         print("Is your secret number " + str(guess) + "?") 
          input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
 
         
 if x == 'c':
-         print("Game over. Your secret number was:" + " " + str(mid))
+         print("Game over. Your secret number was: " + str(guess) + "!")
  
  * Any language, just go for it.
  */
/**
from random import randint
low = 0
guess = randint(low, high)
high = 100


print("Please think of a number between 0 and 100!")
print("Is your secret number " + str(guess) + "?") 
x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

while x != 'c':
    if x == 'h':
        high = guess
        guess = randInt(start, stop)
    elif x == 'l':
        low = guess
        guess = randInt(low, high)
        print("Is your secret number " + str(guess) + "?") 
    else:
         print("Sorry, I did not understand your input.")  
         print("Is your secret number " + str(guess) + "?") 
         input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

        
if x == 'c':
         print("Game over. Your secret number was: " + str(guess) + "!")
 
 * Any language, just go for it.
 */
Pascalos Pascal Anema contributed to this code
1
I made it better for you, I don't know Python, only php and java, but I don't think this causes any errors! :)
MaddieW.py
@@ -1,29 +1,30 @@
 /**
-start = 0
-mid = 50
-stop = 100
+from random import randint
+low = 0
+guess = randint(low, high)
+high = 100
 
 
 print("Please think of a number between 0 and 100!")
-print("Is your secret number" + " " + str(mid) + "?") 
+print("Is your secret number " + str(guess) + "?") 
 x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
 
 while x != 'c':
     if x == 'h':
-        stop = mid
-        mid = int((mid + start)/2)
+        high = guess
+        guess = randInt(start, stop)
     elif x == 'l':
-        start = mid
-        mid = int((mid + stop)/2)
-        print("Is your secret number" + " " + str(mid)) 
+        low = guess
+        guess = randInt(low, high)
+        print("Is your secret number " + str(guess) + "?") 
     else:
          print("Sorry, I did not understand your input.")  
-         print("Is your secret number" + " " + str(mid)) 
+         print("Is your secret number " + str(guess) + "?") 
          input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
 
         
 if x == 'c':
-         print("Game over. Your secret number was:" + " " + str(mid))
+         print("Game over. Your secret number was: " + str(guess) + "!")
  
  * Any language, just go for it.
  */
/**
from random import randint
low = 0
guess = randint(low, high)
high = 100


print("Please think of a number between 0 and 100!")
print("Is your secret number " + str(guess) + "?") 
x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

while x != 'c':
    if x == 'h':
        high = guess
        guess = randInt(start, stop)
    elif x == 'l':
        low = guess
        guess = randInt(low, high)
        print("Is your secret number " + str(guess) + "?") 
    else:
         print("Sorry, I did not understand your input.")  
         print("Is your secret number " + str(guess) + "?") 
         input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

        
if x == 'c':
         print("Game over. Your secret number was: " + str(guess) + "!")
 
 * Any language, just go for it.
 */
Pascalos Pascal Anema contributed to this code
-1
I made it better for you, I don't know Python, only php and java, but I don't think this causes any errors! :)
MaddieW.py
@@ -1,29 +1,30 @@
 /**
-start = 0
-mid = 50
-stop = 100
+from random import randint
+low = 0
+guess = randint(low, high)
+high = 100
 
 
 print("Please think of a number between 0 and 100!")
-print("Is your secret number" + " " + str(mid) + "?") 
+print("Is your secret number " + str(guess) + "?") 
 x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
 
 while x != 'c':
     if x == 'h':
-        stop = mid
-        mid = int((mid + start)/2)
+        high = guess
+        guess = randInt(start, stop)
     elif x == 'l':
-        start = mid
-        mid = int((mid + stop)/2)
-        print("Is your secret number" + " " + str(mid)) 
+        low = guess
+        guess = randInt(low, high)
+        print("Is your secret number " + str(guess) + "?") 
     else:
          print("Sorry, I did not understand your input.")  
-         print("Is your secret number" + " " + str(mid)) 
+         print("Is your secret number " + str(guess) + "?") 
          input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
 
         
 if x == 'c':
-         print("Game over. Your secret number was:" + " " + str(mid))
+         print("Game over. Your secret number was: " + str(guess) + "!")
  
  * Any language, just go for it.
  */
/**
from random import randint
low = 0
guess = randint(low, high)
high = 100


print("Please think of a number between 0 and 100!")
print("Is your secret number " + str(guess) + "?") 
x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

while x != 'c':
    if x == 'h':
        high = guess
        guess = randInt(start, stop)
    elif x == 'l':
        low = guess
        guess = randInt(low, high)
        print("Is your secret number " + str(guess) + "?") 
    else:
         print("Sorry, I did not understand your input.")  
         print("Is your secret number " + str(guess) + "?") 
         input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

        
if x == 'c':
         print("Game over. Your secret number was: " + str(guess) + "!")
 
 * Any language, just go for it.
 */
Pascalos Pascal Anema contributed to this code
0
Ok, something went wrong there, I hope this works now!
MaddieW.py
@@ -1,29 +1,30 @@
 /**
-start = 0
-mid = 50
-stop = 100
+from random import randint
+low = 0
+guess = randInt(low, high)
+high = 100
 
 
 print("Please think of a number between 0 and 100!")
-print("Is your secret number" + " " + str(mid) + "?") 
+print("Is your secret number " + str(guess) + "?") 
 x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
 
 while x != 'c':
     if x == 'h':
-        stop = mid
-        mid = int((mid + start)/2)
+        high = guess
+        guess = randInt(low, high)
     elif x == 'l':
-        start = mid
-        mid = int((mid + stop)/2)
-        print("Is your secret number" + " " + str(mid)) 
+        low = guess
+        guess = randInt(low, high)
+        print("Is your secret number " + str(guess) + "?") 
     else:
          print("Sorry, I did not understand your input.")  
-         print("Is your secret number" + " " + str(mid)) 
+         print("Is your secret number " + str(guess) + "?") 
          input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
 
         
 if x == 'c':
-         print("Game over. Your secret number was:" + " " + str(mid))
+         print("Game over. Your secret number was: " + str(guess) + "!")
  
  * Any language, just go for it.
  */
/**
from random import randint
low = 0
guess = randInt(low, high)
high = 100


print("Please think of a number between 0 and 100!")
print("Is your secret number " + str(guess) + "?") 
x = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

while x != 'c':
    if x == 'h':
        high = guess
        guess = randInt(low, high)
    elif x == 'l':
        low = guess
        guess = randInt(low, high)
        print("Is your secret number " + str(guess) + "?") 
    else:
         print("Sorry, I did not understand your input.")  
         print("Is your secret number " + str(guess) + "?") 
         input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

        
if x == 'c':
         print("Game over. Your secret number was: " + str(guess) + "!")
 
 * Any language, just go for it.
 */

Comment on this code:

Please Login or Register to leave a comment.