I honestly do not know what to do. The questions I got on StackOverflow were about strings and csv inputs, but these variables I am expecting are different. Help would be very much appreciated. I always get ValueError: too many values to unpack (expected 3)
. I want the code to pass the three returned variables from speedCalc() to something I could use in the calculations in spellCheck() (line 35). What should I do?
Here's the code:
#!/usr/bin/env python
import time
import random
import string
import sys
import string
import sys
#from getkey import getkey
def generateTest():
mylines = []
with open ('phrases.txt', 'rt') as myfile: # Open phrases.txt for reading the typing tests
for line in myfile: # For each line of text,
mylines.append(line) # add that line to the list.
# Converting lines of list to select a random phrase
listLen = len(mylines) - 1
return (mylines[random.randint(0,listLen)])
def speedCalc():
# words / time passed (assuming it is 5)
start = time.time()
test = input(print(generateTest()))
end = time.time()
timePassed = (end - start)
generateTestLen = len(generateTest())
return test
return timePassed
return ((generateTestLen/5)/timePassed)*60
def spellCheck():
test, timePassed, wpm = speedCalc()
diff = 0
correctChars = 0
file_A = generateTest()
file_B = test
read_A=open(file_A,'r').read()
read_B=open(file_B,'r').read()
for char_a, char_b in zip(read_A, read_B):
if char_a == char_b:
correctChars = correctChars 1
read_A_len = len(read_A)
correctPercent = (correctChars/read_A_len)*100
errors = read_A_len - correctChars
errorRate = errors/timePassed
netWPM = wpm - errorRate
return correctPercent
return netWPM
correctPercent, netWPM = spellCheck()
print(netWPM)
CodePudding user response:
When a return
statement is reached, a function stops. Any code after the return statement is never executed.
The code
return test
return timePassed
return ((generateTestLen/5)/timePassed)*60
is equivalent to
return test
You should return a tuple of three values. Use
return test, timePassed, ((generateTestLen/5)/timePassed)*60
Adjust spellCheck
accordingly, it has the same problem.
CodePudding user response:
Instead of using:
return test
return timePassed
return ((generateTestLen/5)/timePassed)*60
use
return test, timePassed, ((generateTestLen/5)/timePassed)*60
Your function as you defined in your example just returnes test and exits then. The second and third return is not executed any more.