helloworlddiary-blog
helloworlddiary-blog
[Hello World] Diary
2 posts
Don't wanna be here? Send us removal request.
helloworlddiary-blog · 6 years ago
Text
PYTHON: Rock, Paper, Scissors game
Was checking out Code Club Projects today to see what else I could do on Python and it suggested games like ‘Rock, Paper, Scissors’. I’m still nowhere near a pro in Python programming but I can say that I’m getting the hang of using ‘if’ and ‘else’ statements. However, I will say that, judging from the code I wrote below, I might have gone a little overboard with it. Almost to the point that I’ve literally spammed lines 24 to 35 with about six ‘if’ conditional statements hmm...
To improve:
Find a way to shorten the code (especially in lines 24-35) where it seems the task can be repeated in some loop? i.e. setting the conditions for the winner/loser in the game
I should also mention that lines 30-35 in particular do not work as the ‘YOU LOST! :(’ text gets printed instead of ‘YOU TIED!’ even when the player and computer are tied. Please help if you know why!
1 from random import randint 2 3 print("Let's play Rock, Paper, Scissors!") 4 5 player = input('Rock (r), Paper (p), or Scissors (s)? ') 6 7 if player == 'r': 8    print('You chose rock \n') 9    if player == 'p': 10        print('You chose paper \n') 11 else: 12    print('You chose scissors \n') 13 14 print('Computer chose...') 15 chosen = randint(1, 3) 16 17 if chosen == 1: 18    print('Rock \n') 19    if chosen == 2: 20        print('Paper \n') 21 else: 22    print('Scissors \n') 23 24 if player == 'r' and chosen == 3: 25    print('YOU WON! :)') 26    if player == 'p' and chosen == 1: 27        print('YOU WON! :)') 28        if player == 's' and chosen == 2: 29            print('YOU WON! :)') 30            if player == 'r' and chosen == 1: 31                print('YOU TIED!') 32                if player == 's' and chosen == 3: 33                    print('YOU TIED!') 34                    if player == 'p' and chosen == 2: 35                        print('YOU TIED!') 36 else: 37    print('YOU LOST! :(')
0 notes
helloworlddiary-blog · 6 years ago
Text
PYTHON: Finding the largest number from a given set
Just attended a two-day Python course by Inspizone and I want to give my biggest thanks to Anoop for kick-starting my journey with Python! 
I created this code as a way to practice the theory we learnt. Nothing too special, but it involves the user inputting in three numbers and having the computer tell us the largest value among them.
A breakdown of the code:
'Welcome' title
Define 'int_check(x)' function to check if input is an integer
Code will loop in the order:
Enter first number
Enter second number
Enter third number
State the largest number among the inputs
Option to continue or exit from code
The code will ask the user to re-enter a valid number if the input is not a recognised string.
To improve:
User is required to input the first number again if the second/third inputs are invalid. How do I fix this so that the next input continues from the second/third that was invalid instead of restarting the whole process?
Do comment if you think you can help with any improvements in my code! Sharing is caring :) 
1 # Welcome title 2 print('Welcome') 3 4 # Integer-checking function 5 def int_check(x): 6    if type(x) == int: 7        print(' ') 8    else: 9        print(' ') 10 11 # Game loop 12 while 1: 13    print(' ') 14 15    a = input('Enter your first number: ') 16 17    if a.isnumeric(): 18        a = int(a) 19    else: 20        print(a, 'is not a valid number. Please re-enter.') 21        int_check(a) 22        continue 23 24 25    b = input('Enter your second number: ') 26 27    if b.isnumeric(): 28        b = int(b) 29    else: 30        print(b, 'is not a valid number. Please re-enter a valid set of numbers.') 31        int_check(b) 32        continue 33 34    c = input('Enter your third number: ') 35 36    if c.isnumeric(): 37        c = int(c) 38    else: 39        print(c, 'is not a valid number. Please re-enter a valid set of numbers.') 40        int_check(c) 41        continue 42 43    if a >= b and a >= c: 44        print(a, 'is the largest number') 45    else: 46        if b >= a and b >= c: 47            print(b, 'is the largest number') 48        else: 49            print(c, 'is the largest number') 50 51    print(' ') 52 53    continue_input = input('Enter c to continue, or e to exit: ') 54 55    if continue_input == 'e': 56        break 57    else: 58        continue
1 note · View note