Numbers (3) Flashcards
Write a program that prints a random number between 1 and 10.
from random import randint
x=randint(1,10)
print(“A random number between 1 and 10 is:”, x)
Write a program that prints the value of Pi and the value of sin(0).
from math import sin, pi
print(“Pi is roughly”, pi)
print(“sin(0)=”, sin(0))
Write a program that generates and prints 50 random integers, each between 3 and 6.
from random import randint
for i in range(50):
print(“ A random number between 3 and 6 is: “, randint(5,6))
Write a program that generates a random number, x , between 1 and 50, a random number y between 2 and 5, and computes x square y. (x la puterea y)
from random import randint
x=randint(1,50)
y=randint(2,5)
print(x, y, “The square is:”, x⁎⁎y)
Write a program that generates a random number between 1 and 10 and prints your name that many times.
from random import randint
x=randint(1,10)
print(“Simona “⁎x)
Write a program that generates a random decimal number between 1 and 10 with two decimal places of accuracy. Examples are 1.23, 3.45, 9.80, and 5.00
import random
x=random.uniform(1,10)
print(“The decimal number is:”, round(x, 2))
Write a program that generates 50 random numbers such that the first number is between 1
and 2, the second is between 1 and 3, the third is between 1 and 4, . . . , and the last is between 1 and 51.
from random import randint
for i in range(1,51):
print(randint(1, i+1))
Write a program that asks the user to enter two numbers, x and y , and computes (x-y)/(x+y).
num1=int(input(“Enter the first number: “))
num2=int(input(“Enter the second number: “))
print(num1, num2, (num1-num2)/(num1+num2))
Write a program that asks the user to enter a length in centimeters. If the user enters a negative length, the program should tell the user that the entry is invalid. Otherwise, the program should convert the length to inches and print out the result. There are 2.54 centimeters in an inch.
length=int(input(“Enter a length in centimeteres: “))
if length<=0:
print(“Invalid length!”)
else:
print(length, “The length to inches is:”, round(length/2.54, 2))
Ask the user for a temperature. Then ask them what units, Celsius or Fahrenheit, the temperature is in. Your program should convert the temperature to the other unit. The conversions are F = 9/5C+32 and C=5/9(F-32)
temperature=int(input(“Enter a temperature: “))
unit=input(“Fahrenheit or Celsius?”)
Fahrenheit=x
Celsius=y
print(“Temperature= “, temperature, unit)
if unit==x:
print(“The temperature in Celsius is:”, 9/5⁎(temperature+32))
else:
print(“The temperature in Fahrenheit is:”, round(5/9⁎(temperature-32),2))
Ask the user to enter a temperature in Celsius. The program should print a message based on the temperature:
* If the temperature is less than -273.15, print that the temperature is invalid because it is below absolute zero.
* If it is exactly -273.15, print that the temperature is absolute 0.
* If the temperature is between -273.15 and 0, print that the temperature is below freezing.
* If it is 0, print that the temperature is at the freezing point.
* If it is between 0 and 100, print that the temperature is in the normal range.
* If it is 100, print that the temperature is at the boiling point.
* If it is above 100, print that the temperature is above the boiling point.
temperature=int(input(“Enter a temperature in Celsius: “))
print(“The temperature is:”, temperature)
if temperature<-273.15:
print(“The temperature is invlid because it is below absolute zero.”)
elif temperature==-273.15:
print(“The temperature is absolute zero.”)
elif temperature>-273.15 and temperature<0:
print(“The temperature is below freezin.”)
elif temperature==0:
print(“The temperature is at the freezing point.”)
elif temperature>0 and temperature<100:
print(“The temperature is in the normal range.”)
elif temperature==100:
print(“The temperature is at the boiling point.”)
elif temperature>100:
print(“Temperature is above the boiling point.”)
Write a program that asks the user how many credits they have taken. If they have taken 23 or less, print that the student is a freshman. If they have taken between 24 and 53, print that they are a sophomore. The range for juniors is 54 to 83, and for seniors it is 84 and over.
credits=int(input(“How many credits have you taken?”))
if credits<=23:
print(“Freshman”)
elif credits>=24 and credits<=53:
print(“Sophomore”)
elif credits>=54 and credits<=82:
print(“Junior”)
elif credits>=84:
print(“Senior”)
A store charges $12 per item if you buy less than 10 items. If you buy between 10 and 99 items, the cost is $10 per item. If you buy 100 or more items, the cost is $7 per item. Write a program that asks the user how many items they are buying and prints the total cost.
num_items=int(input(“How many items purchased? “))
if num_items<10:
print(“The total cost is:”, 12⁎num_items)
elif num_items>=10 and num_items<=99:
print(“The total cost is:”, 10⁎num_items)
elif num_items>=100:
print(“The total cost is:”, 7⁎num_items)
Generate a random number between 1 and 100. Ask the user to guess the number and print a message based on whether they get it right or not.
import random
random_num=int(input(“Guess the number between 1 and 100: “))
num=randint(1,100)
if random_num==num:
print(“Correct!”)
else:
print(“Wrong!”)