x = input("Guess a number: ") == 7 print(x) x = input("Guess a number: ") == '7' # compare string and string print(x) x = int(input("Guess a number: ")) == 7 # compare int and int print(x) # what data type does the input() function return? # how does == work? # when should you do casting? # --------------------------------- # y = 12 z = y ** 2 print(z, type(z)) z = y ** 2.0 print(z, type(z)) z = float(y) print(z, type(z)) z = str(y) print(z, type(z)) z = y / 5 # natural division, always result in float print(z, type(z)) z = y // 5 # integer division print(z, type(z)) z = y % 5 # remainder print(z, type(z)) z = 2.0 print('z' * 3, z * 3, '2.0' * 3, str(z) * 3) print((float('2.0') * 3) * 3) print(int(12.5)) print(int('12')) print(int('12.5')) # What is wrong? print(int(float('12.5'))) # int(...string...) works if string looks like int # float(...string...) works if string looks like float # write code to get a number from a user # and evaluate and display whether the given number # is odd or even. # That is, print True if is it even # other print False # print(bool(int(input("Give a number: ")) % 2)==0) ### answer ### # /Users/up3f/PycharmProjects/cs1111/venv/bin/python /Users/up3f/PycharmProjects/cs1111/exam-review/e1-recap-1-solution.py # Guess a number: 6 # False # Guess a number: 4 # False # Guess a number: 19 # False # 144 # Traceback (most recent call last): # 144.0 # 12.0 # 12 # 2.4 # 2 # File "/Users/up3f/PycharmProjects/cs1111/exam-review/e1-recap-1-solution.py", line 42, in # 2 # zzz 6.0 2.02.02.0 2.02.02.0 # print(int('12.5')) # What is wrong? # 18.0 # 12 # ValueError: invalid literal for int() with base 10: '12.5' # 12 # # Process finished with exit code 1