Stackoverflow... pls

86 views
Skip to first unread message

RYAN ZHANG

unread,
Oct 15, 2020, 10:11:06 PM10/15/20
to Pyret Discuss
Hey,
    I am a 9th grader and my school is using pyret. I already know some code, so I tried to create my own projects in pyret in my free time. I know the very basics of python like print("hello world"). This makes me a hacker. Anywayyyyys pyret doesn't seem to have a lot of support. So I have to look through documentation. I hate doing that. I do think pyret error messages are good but... "Either simplify this block to a single expression, or mark the outer expression with block: to indicate this is deliberate." I don't get this. pls help me. This is my code ->

# The function rps(userguess) runs the game.  
fun rps(userguess):
  
  computerguess = num-random(2)

  if userguess == computerguess:
    print('It was a tie')
  else if (userguess == 0) and (computerguess == 1):
    print('computer wins')
  else if (userguess == 0) and (computerguess == 2):
    print('user wins')
  else if (userguess == 1) and (computerguess == 2):
    print('computer wins')
  else if (userguess == 1) and (computerguess == 0):
    print('user wins')
  else if (userguess == 2) and (computerguess == 0):
    print('computer wins')
  else if (userguess == 2) and (computerguess == 1):
    print('user wins')
  end
  
  print(computerguess)
  
end



The game goes 0 equals rock, 1 equals paper, 2 equals scissors. I want to print the computer guess so I know what the computer guessed. But this pops up. "Either simplify this block to a single expression, or mark the outer expression with block: to indicate this is deliberate." I looked up block in the documentation. It doesn't help.

Joe Gibbs Politz

unread,
Oct 15, 2020, 10:51:52 PM10/15/20
to pyret-...@googlegroups.com
Hi Ryan, thanks for your question!

Pyret encourages you to write functions that produce a single value. The rps function you shared has an if expression (that prints) *and* a print expression at the end. Which one is the "answer" to the function? That's what Pyret's asking by complaining about multiple expressions.

Now, there's nothing wrong with what you're trying to do! It's just that in most of the ways we teach Pyret we don't encourage writing functions like this.

I've described the error at the end of this message in more detail.

But before you read it, I want to make suggestions for slightly rethinking your program:
  1. Can you think of a way to make your rps use print *just once*, and print both the message about win/lose/tie AND the computer's guess all at once?
  2. If you can do that, can you write an rps function that doesn't print, but produces a String that has the combined message in it?
  3. If you've used the Design Recipe in your class that uses Pyret, can you use it to help you accomplish (1) and/or (2)?
I think there's a lot to learn from the idea in #2 in particular!


That said, your question deserves a direct answer as well.

Compare these two

fun print-it-once(thing):
  print(thing)
end


fun print-it-twice(thing):
  print(thing)
  print(thing)
end


The second function reports an error similar to what you saw. Pyret is trying to guide you away from writing this kind of program.

But that doesn't mean it's impossible! We can have two statements in the same function (do "two things") by adding block: at the top of the function:

fun print-it-twice(thing) block:
  print(thing)
  print(thing)
end

This version we can call and use. We had to add "block:" to tell Pyret “we know what we're doing, let me use multiple statements here!”

Can you apply that idea to your program to make it have the behavior you wanted originally?




--
You received this message because you are subscribed to the Google Groups "Pyret Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyret-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyret-discuss/e9776e48-9203-45a7-b720-0ebc855d593fn%40googlegroups.com.

RYAN ZHANG

unread,
Oct 16, 2020, 8:31:34 AM10/16/20
to pyret-...@googlegroups.com
OOOOOOOOH I see. Pyret doesn't want multiple answers from one function because it is confusing to read when there is a lot of statements. I shall do 1 and 2. Also, this can be the stack overflow of pyret! Now I can copy and paste this error message in the email search bar to find a very good answer to the problem. Thanks a lot for the help!!

Ryan

To unsubscribe from this group and stop receiving emails from it, send an email to pyret-discus...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Pyret Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyret-discuss/pMjBzzxbOQ4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyret-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyret-discuss/kgbmzkes.0fe230ec-3866-4619-8902-e7f8e1a63c41%40we.are.superhuman.com.
Reply all
Reply to author
Forward
0 new messages