Browse Source

Add files via upload

PythonCoder8 4 years ago
parent
commit
32b4610455
2 changed files with 67 additions and 0 deletions
  1. 35 0
      guess.py
  2. 32 0
      sum_between_numbers.py

+ 35 - 0
guess.py

@@ -0,0 +1,35 @@
+#guess.go converted to python code
+import random
+import sys
+print("LET'S PLAY A GUESSING GAME! YOU WILL HAVE 10 GUESSES to GUESS A RANDOM NUMBER FROM 1 TO 100!\n")
+playAgain = True
+while playAgain:
+    targetNum = random.randint(1,100)
+    success = True
+    for guesses in range(1,11):
+        userguess = input("Enter guess #%d : " %(guesses))
+        try:
+            guess = int(userguess)
+        except:
+            sys.exit("Your guess "+userguess+" is invalid!")
+        if guess < targetNum:
+            print("OOPS! Your guess %d is lesser than the random number!\n" %(guess))
+            success = False
+        elif guess > targetNum:
+            print("OOPS! Your guess %d is greater than the random number!\n" %(guess))
+            success = False
+        elif guess == targetNum:
+            print("Congratulations! You guessed the number!")
+            success = True
+            break
+    if not success:
+        print("Sorry, Game over! The random number is %d" %(targetNum))
+    repeat = ''
+    while repeat != 'Y' and repeat != 'N':
+        repeat = input("Do you want to play again ? (Y/N) : ")
+        if repeat == 'N':
+            playAgain = False
+            print("Ok, thank you for playing. Goodbye!")
+        if repeat == 'Y':
+            playAgain = True
+            print("Ok, here we go again!")           

+ 32 - 0
sum_between_numbers.py

@@ -0,0 +1,32 @@
+#Program to find sum between 1 and certain number
+
+
+#Import sys module for verifying user input
+import sys
+
+#Ask for user input to enter a number
+userInput = input("Enter a number: ")
+
+#If user's input is of float type.....
+if isinstance(userInput, float) == True:
+
+    #Exit program with the message "Decimals are not allowed. Only whole numbers are."
+    sys.exit("Decimals are not allowed. Only whole numbers are.")
+
+#If error occurs when converting the input to int type, exit program with the message "Your input is not a number."
+try:
+    userNumber = int(userInput)
+except:
+    sys.exit("Your input is not a number.")
+
+#Create a variable named "userSum", and assign a value of 0
+userSum=0
+
+#Create for loop ranging from 1 to userNumber (variable declared above) + 1, and inside the for loop, ...
+for i in range(1,userNumber+1):
+
+    #Keep on adding loop variable to "userSum"
+    userSum += i
+
+#Display message showing the sum of numbers between 1 and "userInput"
+print("The sum of numbers from 1 to %s, is %d" %(userInput, userSum))