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 7 reactions

Pascalos Pascal Anema contributed to this code
2
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
3
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
-3
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.
 */
otac_ Dont fuckin worry bout it contributed to this code
-1
Try this--
MaddieW.py
@@ -1,29 +1,32 @@
-/**
+import time
+
 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. ")
+time.sleep(1.23)
+x = None
+tries = 0
 
 while x != 'c':
+    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.")
     if x == 'h':
         stop = mid
-        mid = int((mid + start)/2)
+        mid = int((mid + start) / 2)
+        tries += 1
     elif x == 'l':
         start = mid
-        mid = int((mid + stop)/2)
-        print("Is your secret number" + " " + str(mid)) 
+        mid = int((mid + stop) / 2)
+        print("Is your secret number", str(mid) + "?")
+        tries += 1
+    elif x != 'h' and x != 'l' and x != 'c':
+        print("Sorry, I did not understand your input.")
+        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. ")
     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.
- */
+        pass
+print("Game over! Your secret number was", str(mid))
+print("It took me {} tries to guess it!".format(tries))
import time

start = 0
mid = 50
stop = 100

print("Please think of a number between 0 and 100!")
time.sleep(1.23)
x = None
tries = 0

while x != 'c':
    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.")
    if x == 'h':
        stop = mid
        mid = int((mid + start) / 2)
        tries += 1
    elif x == 'l':
        start = mid
        mid = int((mid + stop) / 2)
        print("Is your secret number", str(mid) + "?")
        tries += 1
    elif x != 'h' and x != 'l' and x != 'c':
        print("Sorry, I did not understand your input.")
        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. ")
    else:
        pass
print("Game over! Your secret number was", str(mid))
print("It took me {} tries to guess it!".format(tries))
otac_ Dont fuckin worry bout it commented on this code
0
try this--

mport time

start = 0
mid = 50
stop = 100

print("Please think of a number between 0 and 100!")
time.sleep(1.23)
x = None
tries = 0

while x != 'c':
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.")
if x == 'h':
stop = mid
mid = int((mid + start) / 2)
tries += 1
elif x == 'l':
start = mid
mid = int((mid + stop) / 2)
print("Is your secret number", str(mid) + "?")
tries += 1
elif x != 'h' and x != 'l' and x != 'c':
print("Sorry, I did not understand your input.")
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. ")
else:
pass
print("Game over! Your secret number was", str(mid))
print("It took me {} tries to guess it!".format(tries))
otac_ Dont fuckin worry bout it commented on this code
2
try this--

mport time

start = 0
mid = 50
stop = 100

print("Please think of a number between 0 and 100!")
time.sleep(1.23)
x = None
tries = 0

while x != 'c':
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.")
if x == 'h':
stop = mid
mid = int((mid + start) / 2)
tries += 1
elif x == 'l':
start = mid
mid = int((mid + stop) / 2)
print("Is your secret number", str(mid) + "?")
tries += 1
elif x != 'h' and x != 'l' and x != 'c':
print("Sorry, I did not understand your input.")
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. ")
else:
pass
print("Game over! Your secret number was", str(mid))
print("It took me {} tries to guess it!".format(tries))

Comment on this code:

Please Login or Register to leave a comment.