Ad
Code
Diff
  • def print_String():
        return lst.reverse()
        
    • def print_String():
    • # Printing the index of a string in the list.
    • lst = ['double', 'switch', 'off', 'on', 'jump']
    • print(lst.index('double'))
    • # Reversing the items in the list
    • print('Reversed List:',lst[::-1])
    • return lst.reverse()

Give the game more features

import random
def roll():
    return random.randint(1,6)
roll()
Code
Diff
  • def print_statements():
        # Printing lines of code:
        # [print(x) for x in ["Hello Mark!","This is my first python script.","Python will be fun to learn!","I am not at COGS","I am at home in my jammies."]]
        print("Hello Mark!\nThis is my first python script.\nPython will be fun to learn!\nI am not at COGS\nI am at home in my jammies.")
    • def print_statements():
    • # Printing lines of code:
    • # [print(x) for x in ["Hello Mark!","This is my first python script.","Python will be fun to learn!","I am not at COGS","I am at home in my jammies."]]
    • print(f"Hello Mark!
    • This is my first python script.
    • Python will be fun to learn!
    • I am not at COGS
    • I am at home in my jammies.")
    • print("Hello Mark!
    • This is my first python script.
    • Python will be fun to learn!
    • I am not at COGS
    • I am at home in my jammies.")

Made it shorter

removed the i variable and just directly printing the index also directly printing the reversed list

Code
Diff
  • def print_String():
        
        # Printing the index of a string in the list.
        lst = ['double', 'switch', 'off', 'on', 'jump']
        print (lst.index('double'))
        
        # Reversing the items in the list
        print ('Reversed List:',lst.reverse())
    • def print_String():
    • # Printing the index of a string in the list.
    • lst = ['double', 'switch', 'off', 'on', 'jump']
    • i = lst.index('double')
    • print (i)
    • print (lst.index('double'))
    • # Reversing the items in the list
    • lst.reverse()
    • print ('Reversed List:', lst)
    • print ('Reversed List:',lst.reverse())
Code
Diff
  • import random
    import sys
    
    # Set up constants
    SECRET_NUMBER = random.randint(1, 100)
    GUESS_LIMIT = 10
    
    
    def main():
        """Main game function."""
        guess_count = 0
        while True:  # main game loop
            while True:  # Validate user input
                guess = input('Guess a number between 1 and 100\n> ')
                if not guess.isdigit() or int(guess) not in range(1, 101):
                    print('Invalid input!')
                    continue
                else:
                    guess_count += 1
                    break
    
            if guess_count == GUESS_LIMIT:
                print(f'Game over! The secret number was {SECRET_NUMBER}.')
                break
    
            if int(guess) == SECRET_NUMBER:
                print(f'Congratulations! You guessed the secret number in {guess_count}/{GUESS_LIMIT} guesses')
                break
            if int(guess) == SECRET_NUMBER and guess_count < 2:
                print(f'Flawless victory!')
    
            if int(guess) > SECRET_NUMBER:
                print('Too high!')
    
            else:
                print('Too low!')
    
        play_again()
    
    
    def play_again():
        """Play again options"""
        while True:
            if input('Do you want to play again (yes or no)?\n> ').lower() not in ['yes', 'no']:
                print('Invalid input!')
                continue
            else:
                break
        if user_input == 'yes':
            return main()
        else:
            print('Thanks for playing, Goodbye!')
            sys.exit()
    
    
    if __name__ == '__main__':
        main()
    
    • import random
    • import sys
    • # Set up constants
    • SECRET_NUMBER = random.randint(1, 100)
    • GUESS_LIMIT = 10
    • def main():
    • """Main game function."""
    • guess_count = 0
    • while True: # main game loop
    • while True: # Validate user input
    • guess = input('Guess a number between 1 and 100\n> ')
    • if not guess.isdigit() or int(guess) not in range(1, 101):
    • print('Invalid input!')
    • continue
    • else:
    • guess_count += 1
    • break
    • if guess_count == GUESS_LIMIT:
    • print(f'Game over! The secret number was {SECRET_NUMBER}.')
    • break
    • if int(guess) == SECRET_NUMBER:
    • print(f'Congratulations! You guessed the secret number in {guess_count} guesses')
    • print(f'Congratulations! You guessed the secret number in {guess_count}/{GUESS_LIMIT} guesses')
    • break
    • if int(guess) == SECRET_NUMBER and guess_count < 2:
    • print(f'Flawless victory!')
    • if int(guess) > SECRET_NUMBER:
    • print('Too high!')
    • else:
    • print('Too low!')
    • play_again()
    • def play_again():
    • """Play again options"""
    • while True:
    • user_input = input('Do you want to play again (yes or no)?
    • > ').lower()
    • if user_input not in ['yes', 'no']:
    • if input('Do you want to play again (yes or no)?
    • > ').lower() not in ['yes', 'no']:
    • print('Invalid input!')
    • continue
    • else:
    • break
    • if user_input == 'yes':
    • return main()
    • else:
    • print('Thanks for playing, Goodbye!')
    • sys.exit()
    • if __name__ == '__main__':
    • main()
Code
Diff
  • def meaning_of_life_is(*args): return 40 + 2;
    • def meaning_of_life_is(*args): return 42;
    • def meaning_of_life_is(*args): return 40 + 2;
Code
Diff
  • def meaning_of_life_is(*args): return 42
    • def meaning_of_life_is(*args): return 40 + 2
    • def meaning_of_life_is(*args): return 42