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 listprint('Reversed List:',lst[::-1])- return lst.reverse()
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(1+1,2)
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(1 + 1, 2)- test.assert_equals(1+1,2)
Give the game more features
import random
def roll():
return random.randint(1,6)
roll()
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1,2)
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 COGSI 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
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())
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()