The Student Room Group

python

>>> roundWinner = "Player"
def PlayRound(winner):
playercard = playerhand.pop(0)
computercard = computerhand.pop(0)

SyntaxError: multiple statements found while compiling a single statement
>>>
whats wrong and what does that syntax mean?
Reply 1
Have you indented your code and what is your function supposed to return? A def should return something when you run it...
Is this in a .py file or did you paste this into the IDLE command line? If you haven't already done so, try putting it into a .py file and running the script -- it may be that the command-line only allows you to run one instruction at a time.
Original post by Spanx
Have you indented your code and what is your function supposed to return? A def should return something when you run it...


Functions don't need to return anything - for example: https://repl.it/repls/AwkwardPuzzledBaitware
Reply 4
Original post by winterscoming
Functions don't need to return anything - for example: https://repl.it/repls/AwkwardPuzzledBaitware
Idented to make sure your function is in it's own block and if you're not defining a function to return something what are you using it for?
import random
def globalvars():
global dogfilename
global playerhand
global computerhand
dogsfilename = "dogs.txt"
playerhand = []
computerhand = []

def menu():
choice()
numberofcards(input)
answer = input()
print("Welcome to Celebrity Dogs")
print("Write A to start game or B to Quit")

answer=False

if answer == ("A"):
print("Let's Play")
answer=True
elif answer == ("B"):
print("Bye then")
else:
print("Invalid answer, Select A or B.")

def numberofcards():
number()
number=int(input("Enter an even number that is less than 30 and more than 4.")
if number < 30 and number > 4 and number % 2 == 0:
print("OK, here are the cards")
else:
print("Invalid")


def setcards(numberofcards): #sout out cards
dogfile=open(dogsfilename,"r")
cards=()
for line in range (0,numberofcards):
dogFromFile = dogsFile.readline()
dogName=dogFromFile.rstrip("\n")
exercise = random.randint(1,5)
intelligence = random.randit(1,100)
Friendliness = random.randit(1,10)
Drool = random.randit(1,10)
cards.add([dogname,exercise,intelligence,friendliness,drool])
dogsFile.close()
random.shuffle (cards)
return cards

def arrangecrads(deck): #dealing cards
while len (deck)>0:
# remove and store the top card from each player's deck
playerhand.append(deck.pop()) # pop the last card from the deck and add it to the player's deck
ComputerHand.append(deck.pop())
def playgame (numberofcards):
deck = setcards(numberofcards)
ArrangeCards(deck)
roundWinner = "Player"
def PlayRound(winner):
playercard = playerhand.pop(0)
computercard = computerhand.pop(0)

#Display the player's card
print("Your card is: ")
displaycard(playercard)
if winner == "player": # player wins
category = choosecategory() # player chooses an attribute/category
else: # computer wins
category=random.randit(1,4) # computer chooses random attribute because a computer can't choose for itself.
# use the category number too find the score of each deck
playerscore = playercard[category]
computerscore = computercard[category]
# set the list of categories/attributes
categories = ("Name", "Excersie", "Intelligence", "Drool")
categoryName = categories[category]
#Use the category number to find the category name from the global variables called categories.
print("The computer's card is: ") # display computer's card
displaycard(ComputerCard)
if winner == "Computer":
print("The computer has chosen", categoryname)
print("Player's",categoryname, " = ",playerscore)
print("Computer's",categoryname, " = ",Computerscore)

can someone check this xx :colondollar:
(edited 5 years ago)
Original post by Spanx
Idented to make sure your function is in it's own block and if you're not defining a function to return something what are you using it for?

It could be all kinds of things - for example, to output something to the screen, or write something into a file/database or sending data to someone's API, or maybe even modifying an object/dictionary which is passed in one of its parameters. It's something you'd probably see quite a lot when a program is broken down in a way where 'write' and 'output' operations are neatly separated away from 'read'/'input' operations and away from any functions which perform any kind of calculations or core logic.
Original post by salma_51999

can someone check this xx

Unfortunately TSR loses all of the formatting when you paste code, so it's impossible to see what's happening. Try pasting it into this website instead:
http://repl.it/languages/python3
Original post by winterscoming
It could be all kinds of things - for example....

ok, thanks xx

https://repl.it/repls/CrushingFamiliarFlatassembler
I copied it out on here and the syntax's are still there

def numberofcards():
number()
number=int(input("Enter an even number that is less than 30 and more than 4.")
if number < 30 and number > 4 and number % 2 == 0: #there ~ the colon is in red
print("OK, here are the cards")
else:
print("Invalid")
(edited 5 years ago)
moved this to computer science :smile:
Can you send the dogs.txt file as well pls
Original post by salma_51999
ok, thanks xx

https://repl.it/repls/CrushingFamiliarFlatassembler
I copied it out on here and the syntax's are still there

def numberofcards():
number()
number=int(input("Enter an even number that is less than 30 and more than 4.")
if number < 30 and number > 4 and number % 2 == 0: #there ~ the colon is in red


You have an error on the previous line -- you're missing a 'closing' parenthesis at the end of the line -- here:
number=int(input("Enter an even number that is less than 30 and more than 4."))

In other words, your int() isn't being closed properly, but the error is showing up on the following line.
Original post by winterscoming
You have an error on the previous line -- you're missing a 'closing' parenthesis at the end of the line -- here:
number=int(input("Enter an even number that is less than 30 and more than 4."))

In other words, your int() isn't being closed properly, but the error is showing up on the following line.

I saw this too but then if you look at the repl link it is closed properly there🤔
Hey guys Now i've checked stuff nothing is outputting when I run.https://repl.it/repls/CrushingFamiliarFlatassembler
Original post by salma_51999
Hey guys Now i've checked stuff nothing is outputting when I run.https://repl.it/repls/CrushingFamiliarFlatassembler


The program creates all of those functions but there's nothing which starts out by actually calling any of them -- you need to start somewhere by calling whichever function or functions are intended to start your program (Possibly the 'menu()' function?)

But the code seems to have a lot of spelling mistakes and other small errors. Is this your real code? have you ever actually run any of this code before?

For example, there is a have a function called 'numberofcards' but on Line 11, the code creates an empty tuple object with the same name 'numberofcards' which hides the name of the function, meaning that you end up with an error on Line 22 instead:


def menu():
numberofcards=() # This line isn't doing anything useful - it's just causing an error further down



When writing code I'd recommend that you only ever write a few lines at any one time before running that code and testing it properly to make sure it works the way you expect. The more code you write, the more likely you are to introduce errors or broken logic and have a much harder time making it work and getting it right.

As things are right now, you've got a lot of errors - If you fix all the errors then make sure you do things in small pieces, making sure you test it along the way to get little bits of it working; otherwise even if you get the code running without errors it may not do what you're expecting. If you end up writing too much code with too many problems then you can reach a point where it's easier just to start again with a blank program.

Writing a big chunk of code all at once without running it or testing it can be a really difficult and time-consuming way of doing things because it means having lots of different bits which could possibly go wrong, likely spending a lot more time trying to fix mistakes and understand what's happening.

Also, you seem to be relying a lot on "global" variables (Variables which are visible and modifiable everywhere in your program) - this will make things harder to understand as your program grows as well since the you can end up with "spaghetti" logic (i..e. hard to see how the variables in your program are changing, and hard to see see where the logic begins and and ends - like a bowl of spaghetti) . There are other ways of working with variables without making them global -- you can use function parameters instead. for example:
https://repl.it/repls/VirtuousFrontCompiler


On a related note, there's a really helpful PDF document here with a few guidelines for helping to find errors:
https://pythonforbiologists.com/29-common-beginner-errors-on-one-page/
(edited 5 years ago)
I did write my code in small chunks but whenever I add another subroutine, there are syntaxes in the previous subroutine.
My teacher told me to add all those global variables - I don't know why though.
I took out numberofcards() in the menu part and added it at the end of the odd even subroutine. Now it works, but if I select B to quit it still asks me to select a number >4 and <30.
Thank you for checking it. :smile:

EDIT: It's fine. I added import sys and sys.exit to stop it.
(edited 5 years ago)

Quick Reply

Latest

Trending

Trending