Having trouble with python simple code running in console [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I ran this code.Then it displayed the follwing in the console.
Traceback (most recent call last): File "", line 13, in NameError: name 'r' is not defined
import math
p = int(raw_input("Please enter deposit amount: \n"))
i = int(raw_input("Please input interest rate: \n"))
t = int(raw_input("Please insert number of years of the invesment: \n"))
interest = raw_input("Do you want a simple or compound interest ? \n")
A = p(1+r*t)
B = p(1+r)^t
if interest == "simple":
print int(float(A))
elif interest == "compound":
print int(float(B))

There were multiple errors and indentation problem ;I commented them where you made mistakes:
p = int(raw_input("Please enter deposit amount: \n"))
r = int(raw_input("Please input interest rate: \n")) #rename i to r
t = int(raw_input("Please insert number of years of the investment: \n"))
interest = raw_input("Do you want a simple or compound interest ? \n")
A = p*(1+r*t) #multiply p with ()
B = p*(1+r)**t #same as B
#** is power
if interest == "simple":
print (float(A)) # you dont need to cast the float again to int
else: #since there is no other conditions it's obvious to print compound
print(float(B))# you dont need to cast the float again to int

If you store the interest rate in i you should not use r in the formula some lines later.

Related

i dont know where the error is in my code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm new to python and cant find where my error is. Python is saying that there is a syntax error but it wont tell me where it is. My task is to make a programme that can count a taxi fare. Here is my code.
distance = input("Enter the distance of the taxi ride in kilometers (km): ")
passengers = input("Enter the amount of passengers from 1 to 5: ")
cost = float(distance) + 3 - 1 * 2
if passengers == 5:
cost = cost * 2
print("The total cost of the journey is £" cost)
else:
print("The total cost of the journey is £" cost)
The error is probably something simple but I can't find it. Thanks in advance.
if you want to print several things with print() function, you need to separate them with commas
try this:
print("The total cost of the journey is £", cost)
You're not concatenating your strings, if you're on python 3.6 or higher use f-strings:
distance = input("Enter the distance of the taxi ride in kilometers (km): ")
passengers = input("Enter the amount of passengers from 1 to 5: ")
cost = float(distance) + 3 - 1 * 2
if passengers == 5:
cost = cost * 2
print(f"The total cost of the journey is £ {cost}")
else:
print(f"The total cost of the journey is £ {cost}")

How to Print a List of Values? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
print("Hello. Please Proceed to Enter a Range of Numbers")
first = int(input("please enter the first number in the range: "))
last = int(input("please enter the last number in the range: "))
numlist=list(range(first,last)
I get an error expected eof while parsing. When I add in print(numlist)
it adds an additional error.
print("Hello. Please Proceed to Enter a Range of Numbers")
first = int(input("please enter the first number in the range: "))
last = int(input("please enter the last number in the range: "))
numlist=list(range(first,last)) #FTFY
for x in numlist:
print(x)
Essentially what this does is after numlist is declared, it traverses it and prints out every value on a new line
Your main problem was that you were missing the second RPAREN on the numlist declaration

Python 3.3: How to create a loop for a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I apologize for the title not being clear, anyways how do I create a loop that will put numbers into a string, what i mean is (this is from the assignment im doing):
Enter number of courses:
Enter grade for course no. 1:
Enter weight for course no. 1:
Enter grade for course no. 2
......
Enter grade for course no. 7
......
Enter weight for course no. 9
So how would i do something like this, i have to create an input statement which has numbers in order (1-infinity but limited by number of courses). Thank you if you need anymore information or for me to clarify let me know.
You can use string formatting:
noc = int(input('Enter number of courses: '))
for i in range(1, noc+1):
grade = input('Enter grade for course no. {}: '.format(i))
weight = input('Enter weight for course no. {}: '.format(i))
#do something with grade and weight here.

How do you convert a user unputted integer into a variable on Python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to make a program to calculate final grades. The user inputs x amount of assignments, and based on that I am looking to make x variables asking for the grade recieved and the weight of each assignment.
For example,
(user selects 20 assignments)
(as input):
enter assignment1 grade received,
enter assignment2 grade received,
...............
enter assignment20 grade received. (and the same for weights)
There needs to be a variable per assignment and I am unsure how to essentially convert an integer into a variable. (lists are not allowed).
Please feel free to offer suggestions. Thanks
numAssigns = input("How many assignments?: ")
marks = {}
for i in range(numAssignments):
mark = input("Enter the grade obtained on assignment %s: " %i)
weight = input("Enter the weight of assignment %s: " %i)/100
if weight not in marks:
marks[weight] = {}
if mark not in marks[weight]:
marks[weight][mark] = 0
marks[weight][mark] += 1
total = 0
for weight in marks:
for mark in marks[weight]
total += mark*weight*marks[weight][mark]
print("From all your assignments, you have %s% of the total grade of the course" %total)

UnboundLocalError: local variable 'print' referenced before assignment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Hey everyone I'm in intro python programming and we're doing out first independent code. The assignment is as follows:
Prompt the user for his or her name. Then prompt the user for two numbers and then perform a mathematical operation of your choice on them. Make sure the program works with decimal numbers, and that you print a full equation in response, not just the result:
Enter a number: 2.3
Enter another number: 3.6
2.3 – 3.6 = -1.3
So I entered:
def main1():
print("This is program 1!")
name = input("Please enter your name: ")
print("Pleased to meet you,", name ,"!") #next line def main2():
print("This is program 2!")
import math
number = input("Enter a number: ")
number = float(number)
numberr = input("Enter another number: ")
numberr = float(numberr)
print = ("number + numberr")
And I keep on getting this:
UnboundLocalError: local variable 'print' referenced before assignment
Help!!
You try to assign a value to print.
You wrote:
print = ("number + numberr")
But you in fact meant:
print(number + numberr)

Categories