summary refs log tree commit diff stats
path: root/python/code/2/4.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/code/2/4.py')
-rw-r--r--python/code/2/4.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/python/code/2/4.py b/python/code/2/4.py
new file mode 100644
index 0000000..21b1bf4
--- /dev/null
+++ b/python/code/2/4.py
@@ -0,0 +1,27 @@
+from random import randrange
+
+def game():
+    secret = randrange(1, 11)
+    for i in range(3):
+        guess = int(input('Enter a guess: '))
+        if guess != secret:
+            print('Wrong guess!', end=' ')
+        else:
+            print(f'Correct guess in {i + 1} {"try" if i == 0 else "tries"}!')
+            return
+        if i != 2:
+            print('Try again.')
+        else:
+            print('Game over!')
+            print(f'Secret value was {secret}')
+
+while True:
+    print('Welcome to number guessing game')
+    print('Try to guess the secret random number between 1 to 10 correctly in 3 tries')
+    game()
+    choice = input('Do you want to try playing again (y/n, default n): ')
+    if choice == '' or choice == 'n':
+        print('Thanks for playing. Bye!')
+        break
+    else:
+        print('Enjoy your next play\n')