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 11 months ago.
Improve this question
var = input()
if var = ("lookup"):
print(input())
var = 0
It says I need a ":" on the second line but there is already one there. What do I do?
I am not sure what your problem is, but this is some code that runs:
var = input()
if var == "lookup":
print(input()) # printing what is input?
var = 0
You were checking if var is "lookup", but as pointed out in the comments, you were missing an "=" sign. Please edit your post to explain exatcly what is needed (post input / output examples).
To compare var you need to use the double equal symbol (==) instead of the single equal symbol (=). The single equal symbol is used for assignment as you did in the first line (var = input('Enter a string: ')).
var = input('Enter a string: ')
if var == "lookup":
print(input('Enter the second string: ')) # input the value then it prints the same
var = 0
print(v"now var is: {var}")
Output:
Enter a string: lookup
Enter the second string: Demo
Demo
now var is: 0
Related
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 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am unable to solve this.
Problem Statement:-
Make use of while loop and print all positive even numbers smaller than or equal to the number given by the user
e.g.:-
User Input: 6
Output: 2, 4, 6
I have tried this but the code is running into an infinite loop.
num = int(input("Please enter an even number: "))
i = 0
while num >= i :
if num % 2 == 0:
print(num)
i = num + 1
Thank you.
Don't use a while loop here, just use a for loop:
num = int(input("Please enter an even number: "))
for nr in range(0, num, 2):
print(nr)
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 last year.
Improve this question
May I know why there be a invalid syntax on my f variable on the first elif loop?
def bsd():
if price_of_properties <= 180000:
price = 180000*0.1
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
elif price_of_properties > 180000 <= 360000:
price = (((180000*0.1) + (price_of_properties - 180000 + (180000* 0.2)))
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
elif price_of_properties > 360000 <= 100000:
price = (((180000*0.1) + (180000*0.2) +(price_of_properties - 180000 + (640000* 0.3)))
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
Your second and third assignments to the price variable have unbalaned parentheses ((more opens than closes).
This will cause an error on the following lines though I think Python 3.11 may fix that, reporting errors more accurately. You can probably fix this by adding a closing parenthesis on each of those two lines. Or removing the first. But check the formulae after you do so, in case you make the wrong choice.
And, yes, the extra opening parenthesis in that first paragraph was intended :-)
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 1 year ago.
Improve this question
I can not assign float to marks. What is the best way to handle this program?
print("enter marks of student obtained in five diff subjects")
float marks = []
for i in range(0,4):
n=input()
marks.append(n)
agg = sum(marks)
print("perentage of student is %0.2f " (agg))
Error screen look like this:
Traceback (most recent call last):
File "C:\Users\windows 7\c.py", line 2
float marks = []
^
SyntaxError: invalid syntax
Inside the for loop
n=input()
marks.append(n)
Is actually appending strings into marks list.
So you should change the code inside the loop to add float numbers into marks or just remove float from float marks = [] to make marks be able to contain strings.
To make this piece of code working you will also need to cast the marks to int or float in order to calculate the agg as following:
print("enter marks of student obtained in five diff subjects")
agg = 0
for i in range(4):
agg = agg + int(input())
print("perentage of student is %0.2f " % (agg))
or alternatively you can do this:
print("enter marks of student obtained in five diff subjects")
marks = []
for i in range(4):
n=input()
marks.append(n)
agg = sum(map(int, marks))
print("perentage of student is %0.2f " % (agg))
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
Here is the code
wagesAmount = float(input("Enter Wages Amount: "))
numberOfHours = float(input("Enter Number Of Hours Worked: "))
totalWagesAmount = (wagesAmount) * (numberOfHours)
if totalWagesAmount > "0":
printtotalWagesAmount
I need to add an else statement as well but I was just trying to troubleshoot this issue first
"0" means it's a string . so when u wanna do if something > 0: thats means that 0 is integer and you say (if somthing > "0" string. so you
wagesAmount = float(input("Enter Wages Amount: "))
numberOfHours = float(input("Enter Number Of Hours Worked: "))
totalWagesAmount = (wagesAmount) * (numberOfHours)
if totalWagesAmount > 0:
print (totalWagesAmount)
You have to remove the "" on 0 because "0" != 0
if totalWagesAmount > 0:
Writing "0" means its string because of dynamic typing in 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 3 years ago.
Improve this question
I am new to programming and have just briefly learned functions in python. I do not understand why print(len(str)) don't work the same as return len(str) in a function.
I have tried both print and return for the last statement of the function and am confused about my understanding of len(). I need some guidance, thank you! Perhaps someone can guide me as to how I can further improve my foundation as I am still pretty new to programming. Thank you!!
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
numDigits(833)
If I change the
print to return len(str_digits) and
numDigits(833) to print(numDigits(833)),
I get my expected answer.
What i expected:
3
Actual result:
3
None
1
2
4
3
In the first case numDigits doesn't return a value from the function, and you only print it inside the function
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
print(numDigits(833))
The output here is
3
None
The 3 comes from print and None comes from the function, and when you print it, it prints None
If you want to return, you need a return statement like return len(str_digits) at the end of the function like so
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
#Return statement
return len(str_digits)
print(numDigits(833))
The output will now be
3
3
Now the first 3 comes from print, and the second 3 comes when you print what numDigits return, which is 3