Python 3 - Get an input to be a function [closed] - python

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

Related

If-else in Python [closed]

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 11 months ago.
Improve this question
Fix this:
for n in range(1,8):
print(n)
if(n>=2 & n<=5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is "+n)
Use and.
and is a Logical AND that returns True if both the operands are true whereas & is a bitwise operator in Python
for n in range(1,8):
print(n)
if(n >= 2 and n <= 5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is ", + n)
1. & is bitwise and operator while and is used for logical and operator.
2. You need to convert integer to string for concatenation.
Corrected code:
for n in range(1,8):
print(n)
if(n>=2 and n<=5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is "+str(n))

Program calculating sum, min, max values of user input [closed]

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 1 year ago.
Improve this question
As the title says, I have created a program that does the following until the user presses the 'X' key. Which then stops and prints out all values in order. I also would like to mention that I can only use a specific compiler through my university, which registers the program as an error/wrong even though it should be correct.
Edit: Added the Status of the compiler.
My question is, what other alternatives can I use to code a similar program, or any recommendations in general.
The requested input:
1
2
X
The requested output:
result:[1, 2]
average:1.50
min:1.00
max:2.00
list1 = []
asknumber = str(0)
while asknumber != 'X':
asknumber = input("Enter number: ")
if asknumber == 'X':
break
list1.append(int(asknumber))
big_value = max(list1)
min_value = min(list1)
average = sum(list1) / len(list1)
print("result:", sorted(list1))
print("average:", f'{average:.2f}')
print("min:", f'{min_value:.2f}')
print("max:", f'{big_value:.2f}')
Since a computer is grading your work, the error is likely because you have spaces after your colons.
Someone suggested to use the following to resolve that issue:
print("result:", sorted(list1), sep='')
However, since you are already using f strings in your print statement, you might as well use them for all of it.
You also do not need to calculate the min, max, and average until the loop ends—and since you break the loop manually, you can just use while True:.
list1 = []
asknumber = str(0)
while True:
asknumber = input("Enter number: ")
if asknumber == 'X':
break
list1.append(int(asknumber))
big_value = max(list1)
min_value = min(list1)
average = sum(list1) / len(list1)
print(f'result:{sorted(list1)}')
print(f'average:{average:.2f}')
print(f'min:{min_value:.2f}')
print(f'max:{big_value:.2f}')

Why doesn't it match any of the IF conditions? [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
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".

No output when trying to print [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 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}')

Can you define a variable inside an if statement? [closed]

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 am trying to create a variable inside an if statement but it won't allow it. Is there any other way to do what I'm trying?
print ('Type numbers 1 or 2')
c = int(input())
if c == 1:
answer = (90) #so the variable "answer" will be set to '90' if the user types "1"
if c == 2:
answer = (50)
print (answer)
The code will run in the expected way if either 1 or 2 are entered as input. But if user inputs another number, you will get an Exception:
NameError: name 'answer' is not defined
To avoid this, you could declare the variable answer before the if statements:
answer = 0 # or a reasonable number
if c == 1:
answer = 90
# ...
Note: The () in answer = (90) are not necessary since it's a single number.
You need to allow the possibility that your input() might be out of your preconceived options:
c = int(input("Type no. 1 or 2: "))
if c==1:
answer=90
print(answer)
elif c==2:
answer =50
print(answer)
else:
print("you input neither 1 nor 2")
Here's a more compact solution using assert:
c = int(input("Type no. 1 or 2: "))
assert c in [1,2], "You input neither 1 nor 2."
answer = 90 if c == 1 else 50
print(answer)

Categories

Resources