global variable python, doesn't run [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I try to run this code in python 3.6
def Arrange(num):
global sec
sec=0
def Digit(nmb):
return nmb%10
def WithoutTheLastDigit(nmb2):
return nmb2//10
def IsEven(even):
if even%2==0:
return True
else:
return False
def AddDigit(number,dig):
number=number*10+dig
while num>0:
Digit(num)
if IsEven(Digit(num))==True:
sec=sec+AddDigit(sec,Digit(num))
WithoutTheLastDigit(num)
print(sec)
and it shows this error:
>>> Arrange(500)
Traceback (most recent call last):
File "", line 1, in
Arrange(500)
File "C:\Users\Yair\Desktop\hw3.py", line 56, in Arrange
sec=sec+AddDigit(sec,Digit(num))
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

Your problem is that the function AddDigit() returns None:
def AddDigit(number,dig):
number=number*10+dig
# this return None by default. mussing `return number`
while num>0:
Digit(num)
if IsEven(Digit(num))==True:
sec=sec+AddDigit(sec,Digit(num)) # This is 0 + None
WithoutTheLastDigit(num)
Note that you code can be simplify greatly with a few things. I didn't change the logic, so you might have some errors here.
def Digit(number):
return number % 10
def WithoutTheLastDigit(number):
return number // 10
def IsEven(number):
return number % 2 == 0:
def AddDigit(number, digit):
return number*10 + digit
while number > 0:
digit = Digit(number)
if IsEven(digit):
sec += AddDigit(sec, digit)
print(sec)

Related

Python Temperature converter output is none [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
Im trying to create a function which will convert temperatures based on the temperature that is being converted from to the temperature which we want. However the output of my code is none when I test it.
def convertTemperature(T, unitFrom, unitTo):
if unitFrom=="Fahrenheit" and unitTo=="Celsius":
T=(T-32)/1.8
return T
elif unitFrom=="Kelvin" and unitTo=="Celcius":
T=T-273.15
return T
elif unitFrom=="Celcius" and unitTo=="Fahrenheit":
T=1.8*T+32
return T
elif unitFrom=="Kelvin" and unitTo=="Fahrenheit":
T=1.8*T-459.67
return T
elif unitFrom=="Celcius" and unitTo=="Kelvin":
T=T+273.15
return T
elif unitFrom=="Fahrenheit" and unitTo=="Kelvin":
T=(T*459.67)/1.8
return T
the unitFrom parameter is the current temperature, T is the temperature and unitTo is the temperature to which I want to convert.
I tried testing it with print(convertTemperature(50.0, "Fahrenheit", "Celcius")) but the output was none.
It is just a spelling mistake Celsius not Celcius
This will work
def convertTemperature(T, unitFrom, unitTo):
if unitFrom=="Fahrenheit" and unitTo=="Celsius":
T=(T-32)/1.8
return T
elif unitFrom=="Kelvin" and unitTo=="Celsius":
T=T-273.15
return T
elif unitFrom=="Celsius" and unitTo=="Fahrenheit":
T=1.8*T+32
return T
elif unitFrom=="Kelvin" and unitTo=="Fahrenheit":
T=1.8*T-459.67
return T
elif unitFrom=="Celsius" and unitTo=="Kelvin":
T=T+273.15
return T
elif unitFrom=="Fahrenheit" and unitTo=="Kelvin":
T=(T*459.67)/1.8
return T
print(convertTemperature(50.0, "Fahrenheit", "Celsius"))
Hi you have use wrong word Celsius
It would be Celcius
Your program is right except spelling mistake

Invalid syntaxt sum(list) >= 2 [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 get an invalid syntax error at:
if sum(severe_symptoms_list) >= 2
I have set the values for the severe and moderate symptoms slots to True and False depending on what answer was given by the user to a YES/NO question. What have i done wrong ?
class ActionHowManySymptoms(Action):
def name(self) -> Text:
return "action_how_many_symptoms"
def run (self, dispatcher, tracker, domain):
severe_symptom1 = tracker.get_slot('severe_symptom_one')
severe_symptom2 = tracker.get_slot('severe_symptom_two')
severe_symptom3 = tracker.get_slot('severe_symptom_three')
severe_symptom4 = tracker.get_slot('severe_symptom_four')
moderate_symptom1 = tracker.get_slot('moderate_symptom_one')
moderate_symptom2 = tracker.get_slot('moderate_symptom_two')
moderate_symptom3 = tracker.get_slot('moderate_symptom_three')
moderate_symptom4 = tracker.get_slot('moderate_symptom_four')
severe_symptoms_list = [severe_symptom1, severe_symptom2, severe_symptom3, severe_symptom4]
moderate_symptoms_list = [moderate_symptom1, moderate_symptom2, moderate_symptom3, moderate_symptom4]
True = 1
if sum(severe_symptoms_list) >= 2
dispatcher.utter_response(response="utter_some_severe_symptoms", tracker)
elif sum(severe_symptoms_list) == 1
dispatcher.utter_response(response="utter_one_severe_symptoms", tracker)
else:
dispatcher.utter_response(response="utter_no_severe_symptoms", tracker)
if sum(moderate_symptoms_list) >= 2
dispatcher.utter_message(response="utter_some_moderate_symptoms")
elif sum(moderate_symptoms_list) == 1
dispatcher.utter_message(response="utter_one_moderate_symptoms")
else:
dispatcher.utter_message(response="utter_no_moderate_symptoms")
return[]
Try to code in a more decent presentable manner. It will help you in the long run
if (sum(severe_symptoms_list) >= 2):

Error: 'str' object is not callable python [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 5 years ago.
Improve this question
I have been stuck dealing with an error in python and have been searching for a while to fix it but to no avail.
Here is the error I am getting
Traceback (most recent call last):
File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 62, in <module>
main()
File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 23, in main
displayOutput(letterCount,middleCharacter,spaceAmount,aReplace)
File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 56, in displayOutput
print('Number of letters:'(letterCount))
TypeError: 'str' object is not callable
Here is the code I have written so far.
def main():
while True:
sentence= userInput()
letterCount= characterCount(sentence)
middleCharacter= middleLetter(sentence)
spaceAmount= spaceCount(sentence)
aReplace= letterReplace(sentence)
displayOutput(letterCount,middleCharacter,spaceAmount,aReplace)
def userInput():
sentence = str(input('Enter a sentence at least 10 letters long, or type STOP to quit:'))
if sentence == 'STOP':
quit()
return sentence
def characterCount(sentence):
letterCount = len(sentence) - sentence.count(' ')
if letterCount < 10:
print('Sorry that is less than 10 letters')
def middleLetter(sentence):
sentence = len(sentence)/2
middleCharacter = [sentence +1]
def spaceCount(sentence):
spaceAmount = sentence.count(' ')
def letterReplace(sentence):
aReplace= sentence.replace("a", "&")
def displayOutput(letterCount,middleCharacter,spaceAmount,aReplace):
print('Number of letters:'(letterCount))
print('Middle letter:'(middleCharacter))
print('Spaces counted:'(spaceAmount))
print('Sentence with letter replaced:'(aReplace))
main()
The solution is probably something simple that I am overlooking but any help would be appreciated.
The error states str object not callable which meant you were treating strings like functions or anything which is callable () in your code.
The issue is in the print statements in displayOutput() function
Corrected code
def displayOutput(letterCount,middleCharacter,spaceAmount,aReplace):
print('Number of letters:',letterCount)
print('Middle letter:',middleCharacter)
print('Spaces counted:',spaceAmount)
print('Sentence with letter replaced:',aReplace)
main()

max() arg is an empty sequence [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
I have written a program for Radix Sort in Python. But when I execute the code I get following error message max() arg is an empty sequence.
Here is my code:
class RadixSort:
num=0
array=[]
def getData(self):
print 'Enter the number of elements you want to enter: '
num=int(input())
print 'Now enter the elements: '
for i in range(0,self.num):
print 'Element ',i+1,': '
value=int(input())
self.array.append(value)
def radixSort(self):
bin=[[],[],[],[],[],[],[],[],[],[]]
r=1
m=max(self.array)
while m>r:
for ele in self.array:
bin[(ele/r)%10].append(ele)
r=r*10
self.array=[]
for i in range(10):
self.array.extend(bin[i])
bin[i]=[]
def displayArray(self):
print ''
for ele in self.array:
print ele
RObject=RadixSort()
RObject.getData()
RObject.radixSort()
RObject.displayArray()
I get this error before entering values in array. How can I solve this?
I think you should replace:
num = int(input())
to
self.num = int(input())
Not superfluous will be to check that the array is not empty:
m = max(self.array) if self.array else 0
You should show the complete traceback. When I run your code I get this:
Enter the number of elements you want to enter:
3
Now enter the elements:
Traceback (most recent call last):
File "radix.py", line 35, in <module>
RObject.radixSort()
File "radix.py", line 17, in radixSort
m=max(self.array)
ValueError: max() arg is an empty sequence
so m=max(self.array) fails because you can't do a max function on an object that doesn't exist. You need to have an init method to create self.array
Why are you using input and not raw_input? You are using python 2.7

[boxx][boxy] Syntax error [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 9 years ago.
Improve this question
I am learning how to code in python with pygame using a book, so I am writing down the code. Yet when I run it, it gives me a syntax error on revealedBoxes[boxx][boxy] = True, I'm
^
not sure what the problem is. Thank you greatly if you could help!
Here is the code:
if boxx != None and Boxy != None:
if not revealedBoxes[boxx][boxy]:
drawHighlightBox(boxx, boxy)
if not revealedBoxes[boxx][boxy] and mouseClicked:
revealBoxesAnimation(mainBoard, [(boxx, boxy)]
revealedBoxes[boxx][boxy] = True
revealBoxesAnimation(mainBoard, [(boxx, boxy)]
You dropped a parenthesis here.
It is a valid statement
>>> revealedBoxes = [[True]]
>>> boxx, boxy = 0, 0
>>> revealedBoxes[boxx][boxy] = True
unless it is used as expression in while, if, ...:
>>> if revealedBoxes[boxx][boxy] = True: pass
File "<stdin>", line 1
if revealedBoxes[boxx][boxy] = True: pass
^
SyntaxError: invalid syntax
>>> while revealedBoxes[boxx][boxy] = True: pass
File "<stdin>", line 1
while revealedBoxes[boxx][boxy] = True: pass
^
SyntaxError: invalid syntax
Did you mean == ?
>>> if revealedBoxes[boxx][boxy] == True: pass
...
UPDATE
The code is missing ).
revealBoxesAnimation(mainBoard, [(boxx, boxy)] # <----
revealedBoxes[boxx][boxy] = True

Categories