Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import random
  2.  
  3. #Asks for name
  4. name = input("What is your name? ")
  5.  
  6. #Greeting
  7. print("Hello ", name)
  8.  
  9. #generate random number
  10. random_num = random.randint(1,20)
  11.  
  12. guess = int(input("Take a guess"))
  13. attempts = 1
  14.  
  15. while (guess != random_num and attempts < 5):
  16. if (guess > random_num):
  17. print("Too high")
  18. else:
  19. print("Too low")
  20. guess = int(input("Guess again"))
  21. attempts = attempts + 1
  22.  
  23. if (guess == random_num):
  24. print("You win")
  25. else:
  26. print("You lose")
  27.  
  28. #Multiplication calculator
  29. #retrieving user input always stores as strings
  30. print("Multiplication calculator")
  31. x = int(input("Enter the first number: "))
  32. y = int(input("Enter the second number: "))
  33.  
  34. answer = x * y
  35. print("The answer is: {}".format(answer))
  36.  
  37.  
  38.  
  39. #Conditionals
  40.  
  41. #Integer
  42. age = int(input("What is your age: "))
  43.  
  44. # Operators: <, <=, >, >=, ==, !=
  45.  
  46. #if 'variable' 'operator' 'variable'
  47. if age >= 18:
  48. print("You are an adult!")
  49. elif age>=13:
  50. print("You are a teengaer!")
  51. else:
  52. print("You are a child!")
  53.  
  54. #additional conditions by using and, & or
  55. if age>=13 and age<=18:
  56. print("Teenager")
  57.  
  58. if age<=13 or age>=13:
  59. print("Not a teenager")
  60.  
  61. colors =["red", "blue", "black"]
  62. color = input("What is your favorite color? ")
  63.  
  64. if (color == "black" or color == "white"):
  65. print("You like shades")
  66. elif (color in colors):
  67. print("Cool!")
  68. else:
  69. print("You have not put in a valid color")
  70.  
  71.  
  72.  
  73. #Looping
  74.  
  75. #For loops - pre determined number of times you want to loop something
  76. '''
  77. for i in range(1,10,2):
  78. print(i)
  79. '''
  80.  
  81. #While loops - runs until condition is not met
  82. #Important to increment conditional variable
  83. counter = 1
  84. while counter <= 5:
  85. print(counter)
  86. counter = counter + 1
  87.  
  88. #Boolean variable = True or False
  89. profitable = True
  90. profit = 50
  91. while profitable != True:
  92. if (profit < 0):
  93. profitable = False
  94.  
  95. #Integers, doubles, lists, dictionaries
  96.  
  97. #Integer
  98. first_variable = 5
  99.  
  100. #Double
  101. x = 5.5
  102.  
  103. #Strings
  104. name = "Jason"
  105.  
  106. #List - associated with multiple values of a variable type... square brackets
  107. colors = ["red", "blue", "green"]
  108. scores = [80, 90, 75]
  109.  
  110. #indexes start from 0 in Python
  111. #print(colors[2])
  112. #print(colors[1:3])
  113.  
  114. #Dictionary - keys paired with values...curly brackets
  115. student1 = {"name" : "Jason", "grade" : 80, "school" : "wlu"}
  116. student2 = {"name" : "Bob", "grade" : 85, "school" : "wlu"}
  117.  
  118. print(student1["name"])
  119. print(student1.keys())
  120. print(student1.values())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement