Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  • Spaces: when defining or using variables, you often have it like var= "value". To avoid confusing it with Bash and keep it consistent, have one space before and after the equals operator, like var = "value".

  • elif vs if: when more than one if statements are used (i.e.--if an operation or function requires two or more conditionals for ONE situation) use elif to describe the other conditionals. This makes your code much more efficient--with if+elif, if one condition is satisfied, the interpreter will not check the other ifs once any of the condition has been satisfied. However, if you use multiple if statements for the same set of conditionals, then it will check the other if statements, even if one has already been satisfied. This can lead to buggy code. See herehere for a similar question.

  • while True: This could be changed to while inputted_password != password_compare to shorten the code and remove the if statement. Your way is just as efficient, however, so it doesn't really matter if it is changed.

  • As mentioned before, functions should go BEFORE your code.

  • Globalizing: When globalizing variables, the global command goes at the top of the function, before any code.

  • Whitespace: I for one don't really care about tabs vs. spaces, but there are some examples of whitespace that are imperative for readability. Keep space between top level sections of code (like imports, functions, and pre-sections of code) to improve readability.

  • Commenting: This code is fairly pleasant to read. However, a little commenting goes a long way! It is also good practice to describe your functions with docstrings.

  • Spaces: when defining or using variables, you often have it like var= "value". To avoid confusing it with Bash and keep it consistent, have one space before and after the equals operator, like var = "value".

  • elif vs if: when more than one if statements are used (i.e.--if an operation or function requires two or more conditionals for ONE situation) use elif to describe the other conditionals. This makes your code much more efficient--with if+elif, if one condition is satisfied, the interpreter will not check the other ifs once any of the condition has been satisfied. However, if you use multiple if statements for the same set of conditionals, then it will check the other if statements, even if one has already been satisfied. This can lead to buggy code. See here for a similar question.

  • while True: This could be changed to while inputted_password != password_compare to shorten the code and remove the if statement. Your way is just as efficient, however, so it doesn't really matter if it is changed.

  • As mentioned before, functions should go BEFORE your code.

  • Globalizing: When globalizing variables, the global command goes at the top of the function, before any code.

  • Whitespace: I for one don't really care about tabs vs. spaces, but there are some examples of whitespace that are imperative for readability. Keep space between top level sections of code (like imports, functions, and pre-sections of code) to improve readability.

  • Commenting: This code is fairly pleasant to read. However, a little commenting goes a long way! It is also good practice to describe your functions with docstrings.

  • Spaces: when defining or using variables, you often have it like var= "value". To avoid confusing it with Bash and keep it consistent, have one space before and after the equals operator, like var = "value".

  • elif vs if: when more than one if statements are used (i.e.--if an operation or function requires two or more conditionals for ONE situation) use elif to describe the other conditionals. This makes your code much more efficient--with if+elif, if one condition is satisfied, the interpreter will not check the other ifs once any of the condition has been satisfied. However, if you use multiple if statements for the same set of conditionals, then it will check the other if statements, even if one has already been satisfied. This can lead to buggy code. See here for a similar question.

  • while True: This could be changed to while inputted_password != password_compare to shorten the code and remove the if statement. Your way is just as efficient, however, so it doesn't really matter if it is changed.

  • As mentioned before, functions should go BEFORE your code.

  • Globalizing: When globalizing variables, the global command goes at the top of the function, before any code.

  • Whitespace: I for one don't really care about tabs vs. spaces, but there are some examples of whitespace that are imperative for readability. Keep space between top level sections of code (like imports, functions, and pre-sections of code) to improve readability.

  • Commenting: This code is fairly pleasant to read. However, a little commenting goes a long way! It is also good practice to describe your functions with docstrings.

deleted 113 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

You did a great job with your code. We hope to see more of your work here!

Best of luck, and happy coding!

You did a great job with your code. We hope to see more of your work here!

Best of luck, and happy coding!

added 268 characters in body
Source Link
  • Spaces: when defining or using variables, you often have it like var= "value". To avoid confusing it with Bash and keep it consistent, have one space before and after the equals operator, like var = "value".

  • elif vs if: when more than one if statements are used (i.e.--if an operation or function requires two or more conditionals for ONE situation) use elif to describe the other conditionals. This makes your code much more efficient--with if+elif, if one condition is satisfied, the interpreter will not check the other ifs once any of the condition has been satisfied. However, if you use multiple if statements for the same set of conditionals, then it will check the other if statements, even if one has already been satisfied. This can lead to buggy code. See here for a similar question.

  • while True: This could be changed to while inputted_password != password_compare to shorten the code and remove the if statement. Your way is just as efficient, however, so it doesn't really matter if it is changed.

  • As mentioned before, functions should go BEFORE your code.

  • Globalizing: When globalizing variables, the global command goes at the top of the function, before any code.

  • Whitespace: I for one don't really care about tabs vs. spaces, but there are some examples of whitespace that are imperative for readability. Keep space between top level sections of code (like imports, functions, and pre-sections of code) to improve readability.

  • Commenting: This code is fairly pleasant to read. However, a little commenting goes a long way! It is also good practice to describe your functions with docstrings.

import time
import random
import sys

def display_intro():  
    '''Playing the game!'''   
    global x_count, y_count
    x = random.randint(min,max)
    y = random.randint(min,max)

    print('\n\n*****NEW ROUND*****')
    time.sleep(.5)
    print("\n\nThe computer's number is... "+str(y))
    time.sleep(1)

    input("\nPress 'Enter' to roll the die...")

    time.sleep(1.2)
    print('\nYour number is... ' +str(x))

    if (x > y):
        time.sleep(.5)
        print("\nYOUR NUMBER WAS HIGHER THAN THE COMPUTER'S! YOU WIN!")
        x_count = x_count + 1

    ifelif (x < y):
        time.sleep(1)
        print ("\nTHE COMPUTER'S NUMBER WAS HIGHER THAN YOURS! YOU LOSE!")
        y_count = y_count + 1

    print('Your score is... ' +str(x_count))
    print('PC score is... '+str(y_count))

# Checking login credentials
Passwordpassword_compare = 'password'
while True:
    passwordinputted_password = input('ENTER PASSWORD:')
    if passwordinputted_password == Passwordpassword_compare:
        time.sleep(0.5)
        print('VERIFYING')
        for i in range(4):
            time.sleep(.3)
            sys.stdout.write(".")
            sys.stdout.flush()
            time.sleep(.8)
        print ('\nWELCOME BACK!')
        break
    print ('TRY AGAIN')

time.sleep(.6)

# Does the user want to play?
answer = input('\nDo you want to play a game?: ')
if answer == 'yes' or 'Yes' or 'Y' or 'y' or 'YES':
    print("Let's Start")
    print ('Please type Your name!: ')
    name = input()
    time.sleep(.6)
    print ('Hello ' +str( name))
    time.sleep(.7)

max = 12
min = 1
x_count = 0
y_count = 0


playagainplay_again = 'yes'
while playagainplay_again == 'yes': 
    displayIntro()
    playagainplay_again = input('\n\nDo you want to play again? (yes or no)')
  • Spaces: when defining or using variables, you often have it like var= "value". To avoid confusing it with Bash and keep it consistent, have one space before and after the equals operator, like var = "value".

  • elif vs if: when more than one if statements are used (i.e.--if an operation or function requires two or more conditionals for ONE situation) use elif to describe the other conditionals. This makes your code much more efficient--with if+elif, if one condition is satisfied, the interpreter will not check the other ifs once any of the condition has been satisfied. However, if you use multiple if statements for the same set of conditionals, then it will check the other if statements, even if one has already been satisfied. This can lead to buggy code. See here for a similar question.

  • As mentioned before, functions should go BEFORE your code.

  • Globalizing: When globalizing variables, the global command goes at the top of the function, before any code.

  • Whitespace: I for one don't really care about tabs vs. spaces, but there are some examples of whitespace that are imperative for readability. Keep space between top level sections of code (like imports, functions, and pre-sections of code) to improve readability.

  • Commenting: This code is fairly pleasant to read. However, a little commenting goes a long way! It is also good practice to describe your functions with docstrings.

import time
import random
import sys

def display_intro():  
    '''Playing the game!'''   
    global x_count, y_count
    x = random.randint(min,max)
    y = random.randint(min,max)

    print('\n\n*****NEW ROUND*****')
    time.sleep(.5)
    print("\n\nThe computer's number is... "+str(y))
    time.sleep(1)

    input("\nPress 'Enter' to roll the die...")

    time.sleep(1.2)
    print('\nYour number is... ' +str(x))

    if (x > y):
        time.sleep(.5)
        print("\nYOUR NUMBER WAS HIGHER THAN THE COMPUTER'S! YOU WIN!")
        x_count = x_count + 1

    if (x < y):
        time.sleep(1)
        print ("\nTHE COMPUTER'S NUMBER WAS HIGHER THAN YOURS! YOU LOSE!")
        y_count = y_count + 1

    print('Your score is... ' +str(x_count))
    print('PC score is... '+str(y_count))

# Checking login credentials
Password = 'password'
while True:
    password = input('ENTER PASSWORD:')
    if password == Password:
        time.sleep(0.5)
        print('VERIFYING')
        for i in range(4):
            time.sleep(.3)
            sys.stdout.write(".")
            sys.stdout.flush()
            time.sleep(.8)
        print ('\nWELCOME BACK!')
        break
    print ('TRY AGAIN')

time.sleep(.6)

# Does the user want to play?
answer = input('\nDo you want to play a game?: ')
if answer == 'yes' or 'Yes' or 'Y' or 'y' or 'YES':
    print("Let's Start")
    print ('Please type Your name!: ')
    name = input()
    time.sleep(.6)
    print ('Hello ' +str( name))
    time.sleep(.7)

max = 12
min = 1
x_count = 0
y_count = 0


playagain = 'yes'
while playagain == 'yes': 
    displayIntro()
    playagain = input('\n\nDo you want to play again? (yes or no)')
  • Spaces: when defining or using variables, you often have it like var= "value". To avoid confusing it with Bash and keep it consistent, have one space before and after the equals operator, like var = "value".

  • elif vs if: when more than one if statements are used (i.e.--if an operation or function requires two or more conditionals for ONE situation) use elif to describe the other conditionals. This makes your code much more efficient--with if+elif, if one condition is satisfied, the interpreter will not check the other ifs once any of the condition has been satisfied. However, if you use multiple if statements for the same set of conditionals, then it will check the other if statements, even if one has already been satisfied. This can lead to buggy code. See here for a similar question.

  • while True: This could be changed to while inputted_password != password_compare to shorten the code and remove the if statement. Your way is just as efficient, however, so it doesn't really matter if it is changed.

  • As mentioned before, functions should go BEFORE your code.

  • Globalizing: When globalizing variables, the global command goes at the top of the function, before any code.

  • Whitespace: I for one don't really care about tabs vs. spaces, but there are some examples of whitespace that are imperative for readability. Keep space between top level sections of code (like imports, functions, and pre-sections of code) to improve readability.

  • Commenting: This code is fairly pleasant to read. However, a little commenting goes a long way! It is also good practice to describe your functions with docstrings.

import time
import random
import sys

def display_intro():  
    '''Playing the game!'''   
    global x_count, y_count
    x = random.randint(min,max)
    y = random.randint(min,max)

    print('\n\n*****NEW ROUND*****')
    time.sleep(.5)
    print("\n\nThe computer's number is... "+str(y))
    time.sleep(1)

    input("\nPress 'Enter' to roll the die...")

    time.sleep(1.2)
    print('\nYour number is... ' +str(x))

    if (x > y):
        time.sleep(.5)
        print("\nYOUR NUMBER WAS HIGHER THAN THE COMPUTER'S! YOU WIN!")
        x_count = x_count + 1

    elif (x < y):
        time.sleep(1)
        print ("\nTHE COMPUTER'S NUMBER WAS HIGHER THAN YOURS! YOU LOSE!")
        y_count = y_count + 1

    print('Your score is... ' +str(x_count))
    print('PC score is... '+str(y_count))

# Checking login credentials
password_compare = 'password'
while True:
    inputted_password = input('ENTER PASSWORD:')
    if inputted_password == password_compare:
        time.sleep(0.5)
        print('VERIFYING')
        for i in range(4):
            time.sleep(.3)
            sys.stdout.write(".")
            sys.stdout.flush()
            time.sleep(.8)
        print ('\nWELCOME BACK!')
        break
    print ('TRY AGAIN')

time.sleep(.6)

# Does the user want to play?
answer = input('\nDo you want to play a game?: ')
if answer == 'yes' or 'Yes' or 'Y' or 'y' or 'YES':
    print("Let's Start")
    print ('Please type Your name!: ')
    name = input()
    time.sleep(.6)
    print ('Hello ' +str( name))
    time.sleep(.7)

max = 12
min = 1
x_count = 0
y_count = 0


play_again = 'yes'
while play_again == 'yes': 
    displayIntro()
    play_again = input('\n\nDo you want to play again? (yes or no)')
Source Link
Loading