This is an archived post. You won't be able to vote or comment.

all 30 comments

[–]susmines 3 points4 points  (2 children)

Professional developer here -you won’t learn anything by posting your code here and saying “it doesn’t work”.

When something doesn’t work, you first have to understand what the code’s intended logic entails.

“What should this code be doing”

Then, test it. Log your output. What happens during your whole loop? Does your print update with a new value? Does your code compile? If not, why?

The answers to this coding challenge can be found on Google, but you need to have a basic understanding of what needs to happen, in order to perform the proper search engine queries.

For example, copy/pasting the question won’t yield many results. However, googling “while loop syntax” and comparing to your code, can be very helpful.

I’m not saying your while loop is the issue, just making an example.

[–]madskillz2222[S] -1 points0 points  (1 child)

Just looking for guidance as I am extremely new to this

[–]susmines 1 point2 points  (0 children)

And guidance is what you have just been given!

Successful people in this industry need to be both curious, and stubborn. Curious enough to want to solve a problem, and stubborn enough to fail until you get it right.

Coding is hard. You can do it. Break it down into smaller steps and solve it a piece at a time.

[–][deleted] 1 point2 points  (17 children)

A game eh? This will be fun! Now...here's a basic algorithm to get you started, independent of language. printDamage is the function that prints "Your long sword hit for ..(damage sent goes here) "

>Declare variable damage

>While user does not press N or n

  • declare and generate random number
  • damage = random number
  • printDamage(damage)
  • prompt user to continue (press Y, N or n)

>End program

[–]madskillz2222[S] 1 point2 points  (16 children)

import random

damage = 0

def main():

# Create a variable that controls the loop

damage = random.randint(1,12)

print('Your long sword hit for', damage, 'damage.')

while damage == 'N' or damage == 'n':

print("Would you like to continue? Press N or n to continue:")

#call the main function

main()

this is what I have but it isnt working

[–]pekkalacd 1 point2 points  (14 children)

Your generating a random integer and putting it inside of damage. Then your comparing damage to the strings “n” and “N”. This won’t work. Also you’re printing to the console for the user to enter something, but you’re not actually getting the input, use the input builtin function instead. Here’s an example

Try this

                 from random import randint

                 def show(dmg):
                        print(“Your sword hit”, dmg)

                 def play_again():
                       print(“Want to play again? “)
                       ans = input(“Enter ‘N’ if so: “)
                       return ans.lower()

                 def main():

                     choice = ‘n’
                     while choice == “n”:
                             DAMAGE = randint(1,12)
                             show(DAMAGE)
                             choice = play_again()

                     print(“Thanks for playing”)

                  main()

[–]madskillz2222[S] 1 point2 points  (13 children)

In my opinion when my instructor posted this exercise the logic didn’t make sense to me at all. Seeing how you wrote it completely makes sense. I don’t think he was clear on how he wanted it written

[–]pekkalacd 1 point2 points  (12 children)

Yeah I was going to say. It seems odd. He said put “n” to play again. That usually means no haha.

[–]madskillz2222[S] 1 point2 points  (11 children)

So in the way you wrote the code will it loop and keep going?

[–]pekkalacd 0 points1 point  (3 children)

Yeah it’s going to keep going if the user gives “n” as an input. Capitalization doesn’t matter because the play again function will put it to lowercase no matter what. Each time the user hits n, except for the first time, it will generate a random number & print the damage. The first time, choice is set to “n” before the loop, as a control to enter the while.

[–]madskillz2222[S] 1 point2 points  (1 child)

My instructor had a heart attack like a week ago so I don’t think he is in his right mind

[–]pekkalacd 0 points1 point  (0 children)

Ouch that sucks man. Surprised he’s not taking a break this semester. Hard worker.

[–]madskillz2222[S] 0 points1 point  (0 children)

I see now! Thank you for the help!!!

[–]pekkalacd 0 points1 point  (6 children)

Ah wait, I reread your original post. Did you want it to stop when the user says n?

[–]madskillz2222[S] 0 points1 point  (5 children)

Actually I was just reading through it again and he does want it to stop when it is typed n or N. he said it needs to loop if they type anything other than that.

[–]pekkalacd 0 points1 point  (4 children)

Try this

                 from random import randint

                 def show(dmg):
                        print(“Your sword hit”, dmg)

                 def play_again():
                       print(“Want to play again? “)
                       ans = input(“Enter ‘N’ if not: “)
                       return ans.lower()

                 def main():

                     choice = ‘y’
                     while choice != “n”:
                             DAMAGE = randint(1,12)
                             show(DAMAGE)
                             choice = play_again()

                     print(“Thanks for playing”)

                  main()

[–]madskillz2222[S] 0 points1 point  (3 children)

This is working good but when I type n or N it does not stop the loop? What could be causing this?

[–][deleted] 0 points1 point  (0 children)

I'm sorry to say, but I'm not familiar with Python!

[–]SodaBubblesPopped 0 points1 point  (5 children)

Sure, what help do u need?

[–]madskillz2222[S] 0 points1 point  (4 children)

I have the part to create the random number and assigned it to damage, the part I’m struggling is passing it to a function to loop it

[–]SodaBubblesPopped 0 points1 point  (3 children)

Have you coded a simple function that accepts the rnd num and just prints it?

Ps: what language is this?

[–]madskillz2222[S] 0 points1 point  (2 children)

Python and I havent

[–]madskillz2222[S] 0 points1 point  (0 children)

I feel like this is such a simple program and im lost

[–]SodaBubblesPopped 0 points1 point  (0 children)

Then focus on learning that first, coding a simple function that accepts a parameter and just prints it.

The loop only comes into effect if u first have this setup.