Just started learning. Trying to figure out how to skip a line in the Times = (Times * int(Random_Number)),
to be something like :
print(Times)
print(Times)
Etc..
Year = input("Now's year : ")
name= input("Enter your name: ")
Initial_age= float(input(("Enter your age: ")))
Final_age= float(Year) + (100 - Initial_age)
Times =(name+ " You're turning"+ " 100 at year " + str(Final_age))
Random_Number = (input("Enter a random number: "))
Times = (Times * int(Random_Number))
print(Times)
If you want to print each NAME You're turning 100 at year XXXX on a new line, like this
John You're turning 100 at year 2109.0
John You're turning 100 at year 2109.0
John You're turning 100 at year 2109.0
John You're turning 100 at year 2109.0
Add a \n at the end here
Times = name + " You're turning 100 at year " + str(Final_age) + "\n"
But the nicest is to use a loop, and a print inside of it
year = int(input("Now's year : "))
name = input("Enter your name: ")
initial_age = int(input("Enter your age: "))
random_number = int(input("Enter a random number: "))
final_age = year + 100 - initial_age
for i in range(random_number):
print(name, "You're turning 100 at year", final_age)
Related
can someone help me troubleshoot my code the reply from going through the code at the end just loops instead of begining the program again completely would be brilliant is someone could help me thanks and i know its very basic and maybe not the best way to do it but as long as it works its fine.
start = input("Do you want to make an order? ")
while start == "Y" or start == "y" :
title = input("Enter book title: ")
sPrice = float(input("Enter price of book: "))
voucher = input("Do you have a voucher? ")
while sPrice >0 :
amount = float(input("Enter amount of books: "))
while amount >= 1 and amount <= 80 :
if amount >= 5 and amount <= 10 :
disc = 5
elif amount >= 11 and amount <= 50 :
disc = 7.5
elif amount >= 51 and amount <= 80 :
disc = 10
else :
disc = 0
base = sPrice * amount
discVal = (base * disc) / 100
dPrice = base - discVal
if voucher == "Y" or voucher == "y" and dPrice >= 25 :
voucherValue = 25
else :
voucherValue = 0
fPrice = dPrice - voucherValue
print (f"Discount rate: {disc}%")
print (f"Discount value: £{discVal:.2f}")
print (f"Total before discount: £{base:.2f}")
print (f"Total after discount: £{dPrice:.2f}")
if voucher == "Y" or voucher == "y" and dPrice >= 25 :
print (f"Value of voucher: £{voucherValue:.2f}")
print (f"Final cost of order: £{fPrice:.2f}")
else :
print ("ERROR - Invalid amount!")
amount = float(input("Enter amount of books: "))
else :
print ("ERROR - Price too low!")
sPrice = float(input("Enter price of book: "))
else :
start = input("Do you want to make an order? ")
You need to include if at two place just to validate the input value.
Change while sPrice >0 : to if sPrice >0 : and line while amount >= 1 and amount <= 80 : to if amount >= 1 and amount <= 80 :
For this problem, I need to "separate" or identify individual sets of 3 different user inputs: a name, an address, and a salary. Then the program needs to find the highest salary and print the name and the address of the person that it belongs to.
I'm not sure how I can do this in Python.
I know I can use max(salary) but how can I print the associated name and address?
I'm trying to do this using a while loop.
Edit:
Ok, so I'm a beginner so I apologize if this is too basic.
This is what I came up with so far.
name = str(raw_input("Input name: "))
address = str(raw_input("Input address: "))
salary = int(raw_input("Input salary or a number < 0 to end program: "))
x = []
while salary > 0:
name = str(raw_input("Input name: "))
address = str(raw_input("Input address: "))
salary = int(raw_input("Input salary or a number < 0 to end program: "))
x.append(salary) #this returns error: 'int' object is not iterable
m = max(salary)
print #should print name, address associated with max(salary)
Thank you
max_salary = None
max_index = None
salary = 1
index = 0
x = []
while salary > 0:
name = raw_input("Input name: ")
address = raw_input("Input address: ")
salary = int(raw_input("Input salary or a number < 0 to end program: "))
if max_salary is None or salary > max_salary:
max_salary = salary
max_index = index
index += 1
x.append("{} {} {}".format(name, address, salary))
if max_index is not None:
print(x[max_index])
Another way (preferred):
x = []
while True:
name = raw_input("Input name: ")
address = raw_input("Input address: ")
salary = int(raw_input("Input salary or a number < 0 to end program: "))
if salary < 0:
break
x.append((name, address, salary))
if x:
print("{} {} {}".format(*max(x, key=lambda t: t[2])))
This should be working fine.
people=[]
elements= int(input('enter the number of people you want: '))
for i in range(elements):
name= input('enter the name of person %d: '% (i+1))
address= input('enter the address: ')
salary= int(input('enter the salary: '))
person={}
person['name']=name
person['address']= address
person['salary']= salary
people.append(person)
# getting the max from all the salaries and the max
sal_max= max([x['salary'] for x in people])
# matching the max salary:
for i in people:
if i['salary']==sal_max:
print (i['name'], i['address'])
name = input("What is your name: ")
age = int(input("How old are you: "))
year = str((2014 - age)+100)
print(name + " will be 100 years old in the year " + year)
I am trying this code in python, but it is not executing, what corrections needs to be done?
From your question
name = input("What is your name: ")
age = int(input("How old are you: "))
year = str((2014 - age)+100)
print(name + " will be 100 years old in the year " + year)
Your code is just fine, change your input to raw_input for name variable.
NOTE: raw_input returns a string, input tries to run as Python expression.
This question already has answers here:
Date Ordinal Output?
(14 answers)
Closed 6 years ago.
I have been developing a small program.
It works perfectly how it is but I want to make the code a bit smaller.
import time, math
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
end= "th"
if age == 3 or age == 13 or age == 23 or age == 33 or age == 43 or age == 53 or age == 63 or age == 73 or age == 83 or age == 93:
end= "rd"
if age == 2 or age == 22 or age == 32 or age == 42 or age == 52 or age == 62 or age == 72 or age == 82 or age == 92:
end= "nd"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
I would like to have an easier way to change the 'end' to other values without having to write them all, can I have it start at 3 then do it for everything 10 more than three.
Use the modulo operator:
if age % 10 == 3:
end = "rd"
elif age % 10 == 2:
end = "nd"
Or use a dict:
ends = {2: "nd", 3: "rd"}
end = ends[age % 10]
You can also use a default:
ends = {1: "st", 2: "nd", 3: "rd"}
end = ends.get(age % 10, "th)
Extract the digit at tenth's place. Then it's straightforward. Though this question belongs to the codereview counterpart of SO.
import time, math
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
tenth_place = age % 10
if tenth_place == 3:
end = "rd"
elif tenth_place == 2:
end = "nd"
else:
end = "th"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
if age in range(3,93,10) :
end = "rd"
You can try this:
if int(age[-1]) == 3:
end= "rd"
if int(age[-1]) == 2:
end= "nd"
Might not be shorter necessarily, but it works (and keeps 12th and 13th proper).
import time, math
name = input("Enter Your Name:")
age = int(input("Enter Your Age:"))
end = "th"
# initializing age lists
list1 = []
list2 = []
# filling list1 with ages 3-93
for i in range(0,10):
list1.append(10*i+3)
# filling list2 with ages 2-92
for i in range(0,10):
list2.append(10*i+2)
# if block to include correct suffix
for ages in list1:
if ages == 13:
end = end;
elif ages == age:
end = "rd"
for ages in list2:
if ages == 12:
end = end
elif ages == age:
end = "nd"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
Thanks For All Of It Guys,
I have also found another flaw and fixed it, here is my current code.
Thanks.
import time, math
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
end= "th"
if age % 10 == 3:
end = "rd"
elif age % 10 == 2:
end = "nd"
elif age % 10 == 1:
end = "st"
if age < 20 and age > 10:
end = "th"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(2)
Thanks,
Bilbo
I'm pretty new to coding so bear with me but how do I make this code work? I'm trying to take the information that is entered by the user and input it into my functions so I can print the total.
def hotel_cost(nights):
return nights * 140
def plane_cost(city):
if city == "Atlanta":
return 220
elif city == "Boston":
return 550
elif city == "Chicago":
return 900
def rental_car_cost(days):
if days >= 7:
return days * 100 - 50
elif days >= 1 and days <= 5:
return days * 100 - 20
a = raw_input("Please choose number of nights: ")
b = raw_input("Please choose which city you are flying to (Atlanta, Boston, Chicago) ")
c = raw_input("Please choose days of rental car use: ")
d = raw_input("Please choose how much spending money you plan on spending: ")
a = nights
b = city
c = days
total = a + b + c + d
print "Your total cost of trip is %d" % total
You need to first convert your numeric input values to numbers and then pass your input values to your functions.
Delete the lines
a = nights
b = city
c = days
Calculate your total with
total = hotel_cost(int(a)) + plane_cost(b) + rental_car_cost(int(c)) + float(d)
I assumed that for the number of nights and rental car days only integers make sense.
I am not sure if this applies in Python version 2. However, you could try this:
a = int(raw_input("Please choose number of nights: ") * 140 )
# Multiplies by 140, but this would defeat the purpose of your functions.
And apply the int function to convert all of your inputs into integers.
You can use input() in Python 2 as it evaluates the input to the correct type. For the functions, you just need to pass your values into them, like this:
def hotel_cost(nights):
return nights * 140
def plane_cost(city):
if city == "Atlanta":
return 220
elif city == "Boston":
return 550
elif city == "Chicago":
return 900
def rental_car_cost(days):
if days >= 7:
return days * 100 - 50
elif days >= 1 and days <= 5:
return days * 100 - 20
nights = input("Please choose number of nights: ")
city = raw_input("Please choose which city you are flying to (Atlanta, Boston, Chicago) ")
rental_duration = input("Please choose days of rental car use: ")
spending_money = input("Please choose how much spending money you plan on spending: ")
total = hotel_cost(nights) + plane_cost(city) + rental_car_cost(rental_duration) + spending_money
print "Your total cost of trip is %d" % total