#๐ Don't understand where to start with this "fruit guessing game" with initially given template
76 messages ยท Page 1 of 1 (latest)
@remote girder
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
from random import choice
#In the game you have 3 attempts to guess the correct fruit
#Info on what the function does (e.g. input, output)
def guess_fruit(random_fruit):
pass #Here you need to code
return -1 ## <-- why and what does that -1 do?
#main
fruits= ['apple','banana','orange','pear','plums','kiwi','strawberry', #list of possible fruit
'pineapple','melon','paprika','mango']
random_fruit= choice(fruits)
fruit= guess_fruit(random_fruit)
if fruit != -1:
print("you guessed correctly")
else:
print("you guessed wrong")
Hey @remote girder!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
from random import choice
#In the game you have 3 attempts to guess the correct fruit
#Info on what the function does (e.g. input, output)
def guessfruit(randomfruit):
pass #Here you need to code
return -1 ## <-- why and what does that -1 do?
#__main
fruits= ['apple','banana','orange','pear','plums','kiwi','strawberry', #list of possible fruit
'pineapple','melon','paprika','mango']
random_fruit= choice(fruits)
fruit= guess_fruit(random_fruit)
if fruit != -1:
print("you guessed correctly")
else:
print("you guessed wrong")```
whats the problem ?
do you know what input() is ?
yeah I know that but am not sure where you need to put it in this instance ?
now youll add code in guessfruit() only
youll need an input so user can guess
and add code so it has 3 tries only u can do that with a for loop for i in range(3) + if else
right! ok but what does that -1 do exactly?
it returns -1
and that value get stored at fruit = guess_fruit() so fruit if user didnt guess will have value -1
wait
so it's giving the correct answer the value of -1?
no any value other than -1 will give correct
ok I think I get it is it ok if I come back with an update and or I get stuck again?
sure
ok so the guess_fruit function should look like this then ?
def guess_fruit(random_fruit):
attempt = input("What is your guess? ")
for i in range(3):
return -1
'''def guess_fruit(random_fruit):
attempt = input("What is your guess? ")
for i in range(3):
return -1'''
attempt = input("What is your guess? ")
for i in range(3):
return -1```
that loop will run 1 time
you didnt make the logic of guessing if attempt == random_fruit
right sure but now it always says correct ? def guess_fruit(random_fruit): attempt = input("What is your guess? ") if attempt == random_fruit: for i in range(3): return -1
Hey @remote girder!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
you need to make the logic inside the for loop
- the attempt
right ok so like this then for i in range(3): attempt = input("What is your guess ") if attempt == random_fruit: return -1
Hey @remote girder!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
.
that will give u wrong
even tho you correct
ok so what would the next step be?
you are done you just need to return smth else other than -1
and then if you didnt guess it right return -1
so I need an else there under that that returns a diffrent value?
yes
ok awesome thank you ๐ it seems to work perfectly now
show me code lemme see the final code
sure give me a sec
#In the game you have 3 attempts to guess the correct fruit
#Info on what the function does (e.g. input, output)
def guess_fruit(random_fruit):
for i in range(3):
attempt = input("What is your guess ")
if attempt == random_fruit:
return -1
else:
return 1
#__main__
fruits= ['apple','banana','orange','pear','plums','kiwi','strawberry', #list of possible fruit
'pineapple','melon','paprika','mango']
random_fruit= choice(fruits)
fruit= guess_fruit(random_fruit)
if fruit != -1:
print("you guessed correctly")
else:
print("you guessed wrong") ```
oh!
add this to the print print(f"you guessed wrong the fruit was {random_fruit)")
ok so like this? if fruit != -1:
print("you guessed correctly")
else:
print(f"you guessed wrong the fruit was {random_fruit}")
ye
ok but how do I implement 3 attempts?
you already did
wait
ah because
you make if else and if the statment is wrong it will go to else therefore it will return so it will stop for loop
add return -1 outside of the loop
and remove the else
that gives me an indent error :/
show
#In the game you have 3 attempts to guess the correct fruit
#Info on what the function does (e.g. input, output)
def guess_fruit(random_fruit):
for i in range(3):
attempt = input("What is your guess ")
if attempt == random_fruit:
return -1
#__main__
fruits= ['apple','banana','orange','pear','plums','kiwi','strawberry', #list of possible fruit
'pineapple','melon','paprika','mango']
random_fruit= choice(fruits)
fruit= guess_fruit(random_fruit)
if fruit != -1:
print("you guessed correctly")
else:
print(f"you guessed wrong the fruit was {random_fruit}")```
ok there it goes it's just colab not updating the cell correctly
wdym
it was saying it was expecting an indent but not anymore
you cant just add a if statement: without smth inside it
if a == 1:
print("")```
no I know it was the return -1 outside it was complaining about
but seems to work now
yea
#In the game you have 3 attempts to guess the correct fruit
#Info on what the function does (e.g. input, output)
def guess_fruit(random_fruit):
for i in range(3):
attempt = input("What is your guess ")
if attempt == random_fruit:
return 1
return -1
#__main__
fruits= ['apple','banana','orange','pear','plums','kiwi','strawberry', #list of possible fruit
'pineapple','melon','paprika','mango']
random_fruit= choice(fruits)
fruit= guess_fruit(random_fruit)
if fruit != -1:
print("you guessed correctly")
else:
print(f"you guessed wrong the fruit was {random_fruit}")```
good
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.