1
1
import random
2
+ import os
2
3
3
- def guess (x ):
4
- random_number = random .randint (1 ,x )
5
- guess = 0
6
- tries = 0
7
- while guess != random_number :
8
-
9
- print ("No. of attempts taken: " + str (tries ))
10
- guess = int (input (f"Guess a number between 1 and { x } : " ))
11
- tries += 1
12
-
13
- z = guess - random_number
14
- print ("\033 c\033 [3J" , end = '' ) #For clearing the console
15
- if z > 500 :
16
- print ("Woah too high, try going much lower" )
17
- elif z > 250 :
18
- print ("Quite high, try going more lower" )
19
- elif z > 125 :
20
- print ("That's high, try going lower" )
21
- elif z > 63 :
22
- print ("That's a bit high, try going lower" )
23
- elif z > 30 :
24
- print ("Almost there, go lower" )
25
- elif z > 15 :
26
- print ("Almost there, slowly go lower" )
27
- elif z > 7 :
28
- print ("That's so close, decrease it a bit" )
29
- elif z > 3 :
30
- print ("Super Close! Carefully decrease it" )
31
- elif z < - 500 :
32
- print ("Woah too low, try going much higher" )
33
- elif z < - 250 :
34
- print ("Quite low, try going more higher" )
35
- elif z < - 125 :
36
- print ("That's low, try going higher" )
37
- elif z < - 63 :
38
- print ("That's a bit low, try going higher" )
39
- elif z < - 30 :
40
- print ("Almost there, go higher" )
41
- elif z < - 15 :
42
- print ("Almost there, slowly go higher" )
43
- elif z < - 7 :
44
- print ("That's so close, increase it a bit" )
45
- elif z < - 3 :
46
- print ("Super Close! Carefully infc it" )
47
- print ("\033 c\033 [3J" , end = '' ) #for clearing the console
48
- print ("You Did It!!! The correct answer is " + str (random_number )) #This statement doesn't get printed idk how to not let it get cleared
4
+ def rguess ():
5
+ random_number = random .randint (1 ,1000 )
6
+ return random_number
49
7
50
- #After correct answer it goes back into the while loop which shouldn't happen.
51
-
52
- guess (1000 )
8
+ random_number = rguess ()
9
+ guess = None
10
+ tries = 0
11
+
12
+ while guess != random_number :
13
+ print ("No. of attempts done: " , tries )
14
+ guess = int (input ("Guess a number between 1 and 1000: " ))
15
+ tries += 1
16
+ z = guess - random_number
17
+ os .system ("cls" ) #For clearing the console
18
+ if z > 500 :
19
+ print ("Woah too high, try going much lower" )
20
+ elif z > 250 :
21
+ print ("Quite high, try going more lower" )
22
+ elif z > 125 :
23
+ print ("That's high, try going lower" )
24
+ elif z > 63 :
25
+ print ("That's a bit high, try going lower" )
26
+ elif z > 30 :
27
+ print ("Almost there, go lower" )
28
+ elif z > 15 :
29
+ print ("Almost there, slowly go lower" )
30
+ elif z > 7 :
31
+ print ("That's so close, decrease it a bit" )
32
+ elif z > 3 :
33
+ print ("Super Close! Carefully decrease it" )
34
+ elif z < - 500 :
35
+ print ("Woah too low, try going much higher" )
36
+ elif z < - 250 :
37
+ print ("Quite low, try going more higher" )
38
+ elif z < - 125 :
39
+ print ("That's low, try going higher" )
40
+ elif z < - 63 :
41
+ print ("That's a bit low, try going higher" )
42
+ elif z < - 30 :
43
+ print ("Almost there, go higher" )
44
+ elif z < - 15 :
45
+ print ("Almost there, slowly go higher" )
46
+ elif z < - 7 :
47
+ print ("That's so close, increase it a bit" )
48
+ elif z < - 3 :
49
+ print ("Super Close! Carefully infc it" )
50
+ #print("nice", end='') #for clearing the console
51
+ print ("You Did It!!! The correct answer is " , random_number ) #This statement doesn't get printed idk how to not let it get cleared
52
+
53
+ #After correct answer it goes back into the while loop which shouldn't happen.
54
+
55
+ # Improvements/Changes by Sohan
56
+ # 1. Added a new module to clear the console since the initial code was quite bad.
57
+ # 2. made subtle imporvements to the code to further improve functionality
0 commit comments