I cannot figure out why I keep receiving invalid syntax. Here is the function I can't figure out:
def coordType():
coordType = input("Automatic [a] or manual [m] message box coordinates?"
if coordType.lower not in ("a", "m"):
print("It must be 'a' or 'm'")
coordType()
elif coordType.lower() == "a":
print("Please put your mouse over the message box")
time.sleep(5)
xc, yc = m.position()
print("Done - " + str(xc) + ", " + str(yc))
elif coordType.lower() == "m":
xcoord()
ycoord()
try to indent everything.
the body of "coordType" is in the same allignment with the title, so it's invalid in python.
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
I'm having a bit of trouble with my class functions. On my class I have 3 different functions but whenever I call one of the functions outside of the class, it only ever calls the first one despite me typing in the correct function name.
This is the class below with the different functions, although I have only included two as I don't want you to have to search through lots of code.
class mage(baseclass):
def __init__(self, name, level, attack, defence, hp):
baseclass.__init__(self, name, level, hp)
self.attack = attack
self.defence = defence
def __str__(self):
return "You are now a Mage, your new stats are:\n Level: {0}\n Attack: {1}\n Defence: {2}\n HP: {3}".format(self.level, self.attack, self.defence, self.hp)
def flamevortex(self, x, y, z):
print("You used Flame Vortex")
time.sleep(1.5)
damageofmove = 3
damagedone = damageofmove*y
damagedoneafterdefence = damagedone - z
x = x - damagedoneafterdefence
print("The monster's health is now " + str(x))
time.sleep(1.5)
return x
def lightningbolt(self, x, y, z):
print("You used Lightning Bolt")
time.sleep(1.5)
damageofmove = 3
damagedone = damageofmove*y
damagedoneafterdefence = damagedone - z
x = x - damagedoneafterdefence
print("The monster's health is now " + str(x))
time.sleep(1.5)
return x
This is the place where I am calling the functions:
if Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX":
monster1.hp = p1.flamevortex(monster1.hp, p1.attack, monster1.defence)
if chosenmove == monsterattacks[0]:
p1.hp = monsterlasersword(p1.hp)
elif chosenmove == monsterattacks[1]:
p1.hp = monsterswipe(p1.hp)
elif chosenmove == monsterattacks[2]:
monster1.hp = monsterregen(monster1.hp)
time.sleep(1.5)
print("After the monster's attacks, your hp is now " + str(p1.hp))
elif Userattack.upper() == "LIGHTNINGBOLT" or "LIGHTNING BOLT":
monster1.hp = p1.lightningbolt(monster1.hp, p1.attack, monster1.defence)
if chosenmove == monsterattacks[0]:
p1.hp = monsterlasersword(p1.hp)
elif chosenmove == monsterattacks[1]:
p1.hp = monsterswipe(p1.hp)
elif chosenmove == monsterattacks[2]:
monster1.hp = monsterregen(monster1.hp)
time.sleep(1.5)
print("After the monster's attacks, your hp is now " + str(p1.hp))
No matter what the user inputs, it only ever calls the first function.
I know this is a lot to process and appreciate any help. Thanks
if Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX": means is userattack.upper() equal to "FLAMEVORTEX", or does the string "FLAME VORTEX" have True value.
Now since empty strings are False and non-empty strings are True, Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX" is always True, and that's not what you meant.
Try: Userattack.upper() == "FLAMEVORTEX" or Userattack.upper()=="FLAME VORTEX"
I am new to programming (About 2 months in to teaching myself). I am attempting to create a game of tic tac toe and use it as an opportunity to practice using functions and passing parameters.
I have got most of what I want to work working (for now I will add AI and a computer opponent) but when one of the human players win the endgame() function is called but it does not work as expected. It calls itself somehow and you have got to say N to end the game three times before the program is terminated. I am unable to see the wood for the trees on this one folks so help would be appreciated.
I know some of my coding will not be great so no trolling please.
Thanks
Shaun
def start():
choices = [" "," "," "," "," "," "," "," "," "]
while checkwin(choices)==False:
board(choices)
getchoice(choices)
def board(choices):
print("+---+---+---+")
print("+ "+choices[0]+" + "+choices[1]+" + "+choices[2]+" +")
print("+---+---+---+")
print("+ "+choices[3]+" + "+choices[4]+" + "+choices[5]+" +")
print("+---+---+---+")
print("+ "+choices[6]+" + "+choices[7]+" + "+choices[8]+" +")
print("+---+---+---+")
def endgame(winner):
print("The winner is "+ winner)
playagain=input("Another game? Y/N")
playagain = playagain.upper()
if playagain == "N":
print("Thanks, hope to see you again soon.")
else:
start()
def getchoice(choices):
userchoice = int(input ("Where would you like to put your X?"))
index = userchoice-1
while choices[index]!=" ":
userchoice = int(input ("That space is taken. Where would you like to put your X?"))
index = userchoice-1
choices[index]="X"
board(choices)
if checkwin(choices)==False:
userchoice = int(input ("Where would you like to put your O?"))
index = userchoice-1
while choices[index]!=" ":
userchoice = int(input ("That space is taken. Where would you like to put your O?"))
index = userchoice-1
choices[index]="O"
checkwin(choices)
return choices
def checkwin (c):
if checkwin1(c)==False:
if checkwin2 (c)==False:
return False
else:
endgame("Player 2")
else:
endgame("Player 1")
def checkwin1(c):
return ((c[0]=="X" and c[1]=="X" and c[2]=="X") or
(c[3]=="X"and c[4]=="X" and c[5] =="X") or
(c[6]=="X"and c[7]=="X" and c[8] =="X") or
(c[0]=="X"and c[3]=="X" and c[6] =="X" )or
(c[1]=="X"and c[4]=="X" and c[7] =="X") or
(c[2]=="X"and c[5]=="X" and c[8] =="X") or
(c[0]=="X"and c[4]=="X" and c[8] =="X") or
(c[6]=="X"and c[4]=="X" and c[2] =="X"))
def checkwin2(c):
return ((c[0]=="O" and c[1]=="O" and c[2]=="O") or
(c[3]=="O"and c[4]=="O" and c[5] =="O") or
(c[6]=="O"and c[7]=="O" and c[8] =="O") or
(c[0]=="O"and c[3]=="O" and c[6] =="O" )or
(c[1]=="O"and c[4]=="O" and c[7] =="O") or
(c[2]=="O"and c[5]=="O" and c[8] =="O") or
(c[0]=="O"and c[4]=="O" and c[8] =="O") or
(c[6]=="O"and c[4]=="O" and c[2] =="O"))
start()
Let's imagine that X wins (because this is probably what caused it to "end") three times.
You then get to if checkwin(choices)==False: within getchoice(),
'checkwin()' happily calls endgame(). endgame() returns, and then getchoice() continues executing.
At the end of getchoice(), checkwin() is called again, with the same result.
After getchoice() returns we get back to while checkwin(choices)==False: within start(), once again with the same result.
Also note that you will see this happening even more times if you actually played multiple games in a row.
Also try making O win, I think you will only have to say no to your prompt twice in that case.
EDIT:
class TicTacToeGame:
INCOMPLETE = 0
WINNER_PLAYER_1 = 1
WINNER_PLAYER_2 = 2
DRAW = 3
SYMBOLS=["X","O"," "]
def __init__(self,player_1, player_2):
self.board = [[-1]*3,[-1]*3,[-1]*3]
self.players = [player_1,player_2]
self.current_turn = 0;
def advance(self):
if self.calcGameState() != TicTacToeGame.INCOMPLETE:
return
while True:
x,y = self.players[self.current_turn].getMove();
if x < 0 or x > 2 or y < 0 or y>2 :
continue
if self.board[y][x] == -1:
break
self.board[y][x] = self.current_turn
self.current_turn += 1
self.current_turn %= 2
def stringify(self):
re = ""
fr = True
for row in self.board:
if fr:
fr=False
else:
re+= "\n" + "---+" * 2 + "---\n"
fe = True
for el in row :
if fe:
fe = False;
else:
re += "|"
re += " " + TicTacToeGame.SYMBOLS[el] + " "
return re
def calcGameState(self):
for i in range(0,3):
#col
if all(self.board[i][j] == self.board[i][0] for j in range(0,3)) and self.board[i][0] != -1:
return self.board[i][0] + 1
#row
if all(self.board[j][i] == self.board[0][i] for j in range(0,3)) and self.board[0][i] != -1:
return self.board[0][i] + 1
if all(self.board[i][i] == self.board[0][0] for i in range(0,3)) and self.board[0][0] != -1:
return self.board[0][0] + 1
if all(self.board[i][2-i] == self.board[0][2] for i in range(0,3)) and self.board[0][2] != -1:
return self.board[0][2] + 1
if all(self.board[i][j] != -1 for i in range(0,3) for j in range(0,3)):
return TicTacToeGame.DRAW
return TicTacToeGame.INCOMPLETE
def stringResult(self):
res = self.calcGameState()
if res == TicTacToeGame.INCOMPLETE:
return "Incomplete"
if res == TicTacToeGame.DRAW:
return "Draw"
return "Player " + self.SYMBOLS[res-1] + " Won!"
class HumanPlayer:
def __init__(self):
self.board = None
def setGame(self,game):
self.game = game
def getMove(self):
print(self.game.stringify())
print("Current turn is: " + self.game.SYMBOLS[self.game.current_turn])
print("enter row for move")
y = input()
print("enter col for move")
x = input()
return int(x)-1,int(y)-1
def playAgain():
playagain=input("Another game? Y/N\n > ")
playagain = playagain.upper()
if playagain == "N":
print("Thanks, hope to see you again soon.")
return False
return True
while True:
p = HumanPlayer()
t = TicTacToeGame(p,p)
p.setGame(t)
while t.calcGameState() == TicTacToeGame.INCOMPLETE:
t.advance()
print(t.stringResult())
if not playAgain():
break;
Currently, I am creating a Caesar Cipher but it is not working correctly, can anyone help at all? The code will be below. At the moment, if the program is run first time (as in, no functions have to be re run) it works perfectly, but when the getKey() function is re run, it returns an error. After the code, the error is shown:
def runProgram():
def choice():
userChoice = input("Do you wish to Encrypt of Decrypt? Enter E or D: ").lower()
if userChoice == "e":
return userChoice
elif userChoice == "d":
return userChoice
else:
print("Invalid Response. Please try again.")
choice()
def getMessage():
userMessage = input("Enter your message: ")
return userMessage
def getKey():
try:
userKey = int(input("Enter a key number (1-26): "))
except:
print("Invalid Option. Please try again.")
getKey()
else:
if userKey < 1 or userKey > 26:
print("Invalid Option. Please try again.")
getKey()
else:
return userKey
def getTranslated(userChoice, message, key):
translated = ""
if userChoice == "e":
for character in message:
num = ord(character)
num += key
translated += chr(num)
savedFile = open('Encrypted.txt', 'w')
savedFile.write(translated)
savedFile.close()
return translated
else:
for character in message:
num = ord(character)
num -= key
translated += chr(num)
return translated
userChoice = choice() #Runs function for encrypt/decrypt selection. Saves choice made.
message = getMessage() #Run function for user to enter message. Saves message.
key = getKey() #Runs function for user to select key. Saves key choice.
translatedMessage = getTranslated(userChoice, message, key) #Runs function to translate message, using the choice, message and key variables)
print("\nTranslation complete: " + translatedMessage)
runProgram()
I have tried to create it error proof during the getKey() function with the try, except and else commands. It will 'Try' to see that the input is an int or not, if it is, it goes to else, but if it isn't an int, then it will rerun the function. If the function is rerun, and an int is entered, this error is given:
This is an example of it working:
Do you wish to Encrypt of Decrypt? Enter E or D: E
Enter your message: Hello
Enter a key number (1-26): 5
Translation complete: Mjqqt
This is an example when the getKey() function must be re run due to an int not being entered:
Do you wish to Encrypt of Decrypt? Enter E or D: E
Enter your message: Hello
Enter a key number (1-26): H
Invalid Option. Please try again.
Enter a key number (1-26): 5
Traceback (most recent call last):
File "C:\Python34\Encryptor2.py", line 54, in
runProgram()
File "C:\Python34\Encryptor2.py", line 52, in runProgram
translatedMessage = getTranslated(userChoice, message, key) #Runs function to translate message, using the choice, message and key variables)
File "C:\Python34\Encryptor2.py", line 35, in getTranslated
num += key
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
As you can see, it re runs the function as I want it too, but the error occurs when adding the key to the ord of character.
The first call to getKey(), with your comment:
key = getKey() #Runs function for user to select key. Saves key choice.
Another place you call it:
if userKey < 1 or userKey > 26:
print("Invalid Option. Please try again.")
getKey()
If you were to write that with the same kind of comment, it would be:
getKey() #Runs function for user to select key. Doesn't save key choice.
What the user types in, comes out of getKey() ... and you aren't keeping track of it, so it vanishes. You then do.
return userKey
userKey is still the H you tried to convert to int, the one that failed. You didn't get rid of it, so it's still there.
The better solution is to rework the shape of your code, so getKey() never calls getKey() inside itself. Do the error checking outside, perhaps, like this kind of shape:
def getKey():
prompt user for key
try to convert to int and return the int
if it fails, return None as an indication that getting the key went wrong.
key = None #set some wrong placeholder
while (key is None) or (key < 1) or (key > 26):
key = getKey()
change your input to raw_input
just use the maketrans and translate functions that basically encrypt or decrypt the message for you.they make for a very short and efficient solution to the problem
message = input('enter message').lower()
offset = int(input('enter offset (enter a negative number to decrypt)'))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
enc_alphabet = (alphabet[alphabet.index(alphabet[offset]):len(alphabet)])+ alphabet[0:offset]
data = str.maketrans(alphabet,enc_alphabet)
final_message = str.translate(message, data)
print(final_message)
This code really doesn't need to be this complicated at all, if you just use regex the code will be much shorter but (in my opinion) way better.
Here's a code I created for Caesar cipher encrypting, decrypting and using a shift of the user's choice using regex.
import re
def caesar(plain_text, shift):
cipherText = ''
for ch in plain_text:
stayInAlphabet = ord(ch) + shift
if ch.islower():
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
elif stayInAlphabet < ord('a'):
stayInAlphabet += 26
elif ch.isupper():
if stayInAlphabet > ord('Z'):
stayInAlphabet -= 26
elif stayInAlphabet < ord('A'):
stayInAlphabet += 26
finalLetter = chr(stayInAlphabet)
cipherText += finalLetter
print(cipherText)
return cipherText
selection = input ("encrypt/decrypt ")
if selection == 'encrypt':
plainText = input("What is your plaintext? ")
shift = (int(input("What is your shift? ")))%26
caesar(plainText, shift)
else:
plainText = input("What is your plaintext? ")
shift = ((int(input("What is your shift? ")))%26)*-1
caesar(plainText, shift)
In brief, having written some functions to save typing I'm trying to set up a vmap[ping] that will allow me to select something I typed, and pass this selection to a function (since typing the function call on the command line, typing the parameters (with quotes), and escaping backslashes etc... counteracts most of the time saved by calling functions)
For (a simple) example, supposing I had the following function
func Test(iStr)
if a:iStr[0] =~ [a-zA-z]
echo "hello"
else
echo "hello world"
endif
endfunc
I'd like to be able to visual select some text and then with some key mapping, F2 say, it will call Test(iStr) with the selection serving as the parameter iStr
I believe, that with more work (i.e. some way to specify that what I've selected should be inside Test()), the following
vmap <F2> :call Test()
is what I'm after. Thing is I've tried a number of variants (guesswork plus a little bit of dodgy inference from :help map) and I'm not getting anything useful. Update, I've tried using a helper function Test2() with just
call Test(<C-W>)
as its body...somehow I think I need to grab hold of the word under my cursor (somehow) and then I'm done - since I can pass that to Test(...) from within Test2
As for an actual example of a function I want to call, the following (incomplete yet) function (and helper functions) would allow me to convert an expression of the form, say,
f_k^{(j)}g
to
f_1^{(j)}g, f_2^{(j)}g, \dots, f_{n-1}^{(j)}g, f_n^{(j)}g
In terms of a procedure I want to
a) type the repeated term in vim
b) visually select it
c) hit some mapping key that will call SumOrSequence(iExpression, iIndex)
d) provide "k" as a parameter
e) press enter
f) see the change made by SumOrSequence(...)
The code for SumOrSequence(...) is as follows:
func SumOrSequence(iExpression, iIndex)
"need to check validity of these - maybe set a default
let default = Interrogate("do with defaults? yes [y] (2,1,n,0,\",\"), yes but specify last term [d[a-Z]], no [n]")
if default == "y"
let leftTerms = 2
let rightTerms = 1
let lastTermIndex = "n"
let firstTermIndex = 0
let operator = ","
let dotType = "\\dots"
elseif default =~ 'd[a-zA-Z]'
let leftTerms = 2
let rightTerms = 1
let lastTermIndex = default[1]
let firstTermIndex = 0
let operator = Interrogate("what separates terms? add [+], subtract [-], times [*], comma [,], ampersand [&]?")
let dotType = "\\cdots"
else "so n or anything else
let leftTerms = InterrogateNumber("how many terms before dots? ")
let rightTerms = InterrogateNumber("how many terms after dots? ")
let lastTermIndex = Interrogate("what is last term index?")
let firstTermIndex = Interrogate("what is first term index?")
let operator = Interrogate("what separates terms? add [+], subtract [-], times [*], comma [,], ampersand [&]?") "need to check only any of these provided
let dotType = "\\cdots"
endif
if operator == ","
let dotType = "\\dots"
endif
if operator == "*"
let operator = "\\times"
endif
let leftCount = 0
let oExpression = ""
while leftCount < leftTerms
if leftCount > 0
let oExpression .= operator . " "
endif
let oExpression .= ReplaceIndex(a:iExpression, a:iIndex, leftCount,1)
let leftCount += 1
endwhile
let oExpression .= operator . " " . dotType . " "
let rightCount = rightTerms-1
while rightCount > 0
"here we are going to be counting backwards from some number denoting number of terms - may need to know if we actually have a number!
echo "decrement: " . HandleDecrement(lastTermIndex, rightCount)
let oExpression .= operator . " " . ReplaceIndex(a:iExpression, a:iIndex, HandleDecrement(lastTermIndex, rightCount),1)
let rightCount -= 1
endwhile
let oExpression .= operator . " " . ReplaceIndex(a:iExpression, a:iIndex, lastTermIndex,0)
echo oExpression
endfunc
func ReplaceIndex(iExpression, iIndex, iReplacement, iInsertBraces)
"the game we play here is to search for iIndex in such form that it is not part of any other string
"We should expect this to be the case if the character to the left or right of the index is not in [A-z] (or just to the right if a greek char)
let oExpression = ""
let strEndPosition = strlen(a:iExpression) - 1
let currPosition = 0
let indexLen = strlen(a:iIndex)
while currPosition <= strEndPosition
let indexCounter = 0
let foundIndex = 1
while indexCounter < indexLen
if a:iExpression[currPosition + indexCounter] == a:iIndex[indexCounter]
if a:iExpression[currPosition + indexLen] =~ '[a-zA-Z]'
let foundIndex = 0
let indexCounter = indexLen
elseif a:iExpression[currPosition -1] =~ '[a-zA-Z]' && a:iExpression[currPosition] != "\\"
let foundIndex = 0
let indexCounter = indexLen
else
let indexCounter+=1
endif
else
let indexCounter = indexLen
let foundIndex = 0
endif
endwhile
if foundIndex == 0
let oExpression .= a:iExpression[currPosition]
let currPosition+=1
else
if a:iInsertBraces == 1
let oExpression .= "{" . a:iReplacement . "}"
else
let oExpression .= a:iReplacement
endif
let currPosition+=indexLen
endif
endwhile
echo "oExpression: " . oExpression
return oExpression
endfunc
func HandleIncrement(iExpression, iIncrement)
"and what about negative numbers for iExpression!??? not handling these yet :[
let oExpression = ""
if !(a:iExpression[0] =~ '[0-9]') || a:iExpression < 10 && strlen(a:iExpression) > 1
let oExpression = a:iExpression . " + " . a:iIncrement
else
let oExpression = a:iExpression + a:iIncrement
endif
echo oExpression
return oExpression
endfunc
func HandleDecrement(iExpression, iIncrement)
"TODO and what about negative numbers for iExpression!??? not handling these yet :[
let oExpression = ""
if !(a:iExpression[0] =~ '[0-9]') || a:iExpression < 10 && strlen(a:iExpression) > 1
let oExpression = a:iExpression . " - " . a:iIncrement
else
let oExpression = a:iExpression - a:iIncrement
endif
echo oExpression
return oExpression
endfunc
func Interrogate(iQuestion)
call inputsave()
let answer = input(a:iQuestion)
call inputrestore()
return answer
endfunc
func InterrogateNumber(iQuestion)
call inputsave()
let answer = input(a:iQuestion)
call inputrestore()
"TODO what if negative number??
if !(answer[0] =~ '[0-9]')
let answer = InterrogateNumber(a:iQuestion . " you didn't enter a numerical value ")
endif
return answer
endfunc
As regards the mapping bit, I know it looks like I haven't done too much work but assuming I have lots more digging ahead of me to find the answer myself, can anyone help?
Update. Ok, I have something that works in a clumsy sort of way, i.e. if I define the following helperfunction:
func SumOrSequenceHelper()
let oIndex = Interrogate("index variable? ")
"go to last thing visually selected (I think!), yank it (putting it in the " register), then fetch it via oParam. Then pass this off to SumOrSequence
execute "normal! gvy"
let oExpression = getreg('"')
call SumOrSequence(oExpression, oIndex)
endfunc
vnoremap <F6> :call SumOrSequenceHelper()
then all is well, and I can just do an execute command to replace what I selected with what I got from SumOrSequence(...)
Would be grateful for any improvements but for all intents and purposes this one is solved :]
You could use a helper function like this:
func! GetSelectedText()
normal gv"xy
let result = getreg("x")
normal gv
return result
endfunc
vnoremap <F6> :call MyFunc(GetSelectedText())<cr>
There is also :com -range, which can register a custom command that operates on a selection, but the interface is line oriented.
Use the selection register to call the function with whatever you have visually selected.
vnoremap <F6> :call Test(#*)<CR>
If you just want to grab hold of the word under the cursor in Normal mode you can use this, which yanks to the selection register then uses the value. (You could yank to any named register instead , e.g. the a register with "ay and #a.)
noremap <S-F6> "*yaw:call Test(#*)<CR>
By the way these don't work with your Test function, but just calling it doesn't seem to work either?
:call Test("fred")
Error detected while processing function Test:
line 1:
E121: Undefined variable: a
E15: Invalid expression: a:iStr[0] =~ [a-zA-z]
They work with this test function:
function Test(iStr)
echo a:iStr
endfunction
Question: I have a program that solves a quadratic equation. The program gives real solutions only. How do I perform the quality testing of the program? Do you need to ask me for some extra input parameters?
Create test cases, and check the result of your program against the expected result (which is calculated externally) in the test case.
The test cases can cover several ordinary cases, together with special cases, such as when the coefficient is 0, or the discriminant is < 0, = 0, near 0. When you compare the result, make sure you handle the comparison properly (since the result is floating point numbers).
# "quadratic-rb.rb" Code by RRB, dated April 2014. email ab_z#yahoo.com
class Quadratic
def input
print "Enter the value of a: "
$a = gets.to_f
print "Enter the value of b: "
$b = gets.to_f
print "Enter the value of c: "
$c = gets.to_f
end
def announcement #Method to display Equation
puts "The formula is " + $a.to_s + "x^2 + " + $b.to_s + "x + " + $c.to_s + "=0"
end
def result #Method to solve the equation and display answer
if ($b**2-4*$a*$c)>0
x1=(((Math.sqrt($b**2-4*$a*$c))-($b))/(2*$a))
x2=(-(((Math.sqrt($b**2-4*$a*$c))-($b))/(2*$a)))
puts "The values of x1 and x2 are " +x1.to_s + " and " + x2.to_s
else
puts "x1 and x2 are imaginary numbers"
end
end
Quadratic_solver = Quadratic.new
Quadratic_solver.input
Quadratic_solver.announcement
Quadratic_solver.result
end