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
Practicing some logic and wondering why none of my conditions are not being met even after entering valid input. I enter 0, 6 or 12 and the result remains the same where it says it does not recognize any choice I selected. This is the code:
import os
remain_bal = 0
user_name = str(input("Please enter your name"))
while user_name != "END":
final_price = float(input("enter the final cost of the device you purchased"))
code_plan = str(input("Please enter the code"))
payment_plan = str(input("Which payment plan are you on? Enter 6 for six months plan or 12 for twelve months plan or 0 for full"))
num_payments = int(input("How many payments have you made? (Enter integer values)"))
if payment_plan == 0:
remain_bal = (final_price - final_price)
elif payment_plan == 6:
remain_bal = final_price - (final_price / 6) * num_payments
elif payment_plan == 12:
remain_bal = final_price - (final_price / 12) * num_payments
else:
print("The plan you selected is not on the list select 0, 6 or 12")
print("Your balance remaining is", remain_bal)
user_name = str(input("Please enter your name"))
This is the output:
What logic am I overlooking?
You have the variable payment_plan as a string, but are comparing it to integer 6, and objects of different classes are not equivalent. Either change the variable to an int like your other input variables, or change the test to "6".
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 1 year ago.
Improve this question
I want to make a code that generates random numbers from 3 different choices that the user can pick from but the code just doesn't recognize the variable I put to represent those choices. Here is what I've got so far:
import random
choose = int(input("1, 2 or 3? "))
if choose == 1:
num == random.randint(1, 10)
elif choose == 2:
num == random.randint(10, 50)
elif choose == 3:
num == random.randint(1, 100)
else:
print("Invalid input")
print(num)
Can someone help?
There is some confusion. == is for comparison like is a equal to b? and = is for assignment. So, just replace to this num = random.randint(10, 50).
Also, you should consider moving print(num) inside the if...elif...else statements as it can raise an error when invalid input is provided.
import random
choose = int(input("1, 2 or 3? "))
if choose == 1:
num = random.randint(1, 10)
print("Your Number: "num)
elif choose == 2:
num = random.randint(10, 50)
print("Your Number: "num)
elif choose == 3:
num =random.randint(1, 100)
print("Your Number: "num)
else:
print("Invalid input")
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 have to use loop and simple code, I can not use functions. I get an error on my first line of calculations and I am not sure why?
import random
#Input
winning_number = random.randint(10,100)
guessed_number = int(input('Enter your lottery pick (2 digits) or -999 to quit:')
#Calculations
first_winning_number = winning_number // 10
second_winning_number = winning_number % 10
first_guessed_number = guessed_number // 10
second_guessed_number = guessed_number % 10
#If Statements
while guessed_number != -999:
if guessed_number == winning_number:
print('Exact Match: You win $10,000')
if first_guessed_number == second_winning_number and second_guessed_number == first_winning_number:
print('Match all digits: You win $3,000')
if first_guessed_digit == second_winning_digit:
print('Match one digit: You win $1,000')
if first_guessed_digit == first_winning_digit:
print('Match one digit: You win $1,000')
if second_guessed_digit == second_winning_digit:
print('Match one digit: You win $1,000')
if second_guessed_digit == first_winning_digit:
print('Match one digit: You win $1,000')
else:
print(guessed_number)
You're missing the second parenthesis on your 3rd line.
Its
guessed_number = int(input('Enter your lottery pick (2 digits) or -999 to quit:'))
not
guessed_number = int(input('Enter your lottery pick (2 digits) or -999 to quit:')
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'm trying to write a python code in which I input a number, which should be a multiple of 3, and calculate the sum of each digit's power of 3,
ig: input'12' => multiple of 3
should return 1^3 +2^3 = 9
I tried this script and didn't get anything when I run it, what's the problem
number= input('give a number: ')
TT=list(number)
if int(number)==0:
print('wrong number')
elif int(number)%3:
for x in (range(len(TT))-1):
aggregate=sum(int(TT[x])**3)
print(f'the aggregate of {number} is {aggregate}')
None of the conditions are true, so no value ever gets displayed. If you input 12, then 12 % 3 equals 0 (False).
Take a look at the python truth value testing
You should update this line:
elif int(number)%3 == 0:
Now, the whole code needs a revision to get the result that you want. Let me make some changes:
number= input('give a number: ')
aggregate = 0
if int(number)==0:
print('wrong number')
elif int(number) % 3 == 0:
for x in number:
aggregate += int(x) ** 3
print(f'the aggregate of {number} is {aggregate}')
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I am trying to insert user input into a list until the user enters '0'. Right now, it won't exit out of the while loop once I enter 0.
Here's my code:
num_array = []
inputnum = raw_input('Please enter a number: ')
num_array.append(inputnum)
while inputnum != 0:
inputnum = raw_input('Please enter a number: ')
num_array.append(inputnum)
for i in range(len(num_array) - 1):
print(num_array[i] + ' + ')
print(num_array[total - 1] + ' = ' + sum(num_array))
Try check:
num_array = []
# raw_input return value as string,int convert it to string.
inputnum = int(raw_input('Please enter a number: '))
num_array.append(inputnum)
# earlier check for string '0' to interger 0 so the condition returned true but now int() converted inputnum to integer.
while inputnum != 0:
inputnum = int(raw_input('Please enter a number: '))
num_array.append(inputnum)
for i in range(len(num_array) - 1):
print("%d +" % num_array[i])
# sum() gave exception because earlier num_array had string type but int() converted all the value to integer.
print("%d = %d" % (num_array[len(num_array) - 1],sum(num_array)))
Try using
while inputnum != '0':
because the raw_input function returns a string, not integer. However, if your input_num needs to be an integer then follow the above solution.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to get this input to become a function to use later on when buying/selling shares in a program. Is this possible? For example, later on I might do:
if shareOne >= 50 //where >= is shareQuestionBuy or shareQuestionSell
CODE BELOW
#Input of share code
shareCode = str(input("What is your share code? "))
if isValid(shareCode):
print("\nShare code", str(shareCode), "is valid\n")
else:
print("\nERROR! Share code", str(shareCode), " invalid. Share code too short/long or contains alpha/unicode characters.\n Program will now terminate\n")
quit()
shareQuestionBuy = bool(input("Are shares bought by" + str(shareCode) + "bought when they are below the limit price (True) or greater than/equal to the limit price (False)? (True/False only): "))
if shareQuestionBuy == True:
print("Shares will now only be bought if they are below the limit price")
shareQuestionBuy = <
elif shareQuestionBuy == False:
print("Shares will now only be bought if they are greater than/equal to the limit price")
shareQuestionBuy = <=
operator
3>> import operator
3>> foo = operator.gt
3>> foo(3, 2)
True