Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,205,666 members, 7,993,341 topics. Date: Monday, 04 November 2024 at 10:41 AM |
Nairaland Forum / Science/Technology / Programming / My Python Guessing Game... Pythonistas Please Come In (2786 Views)
How Can I Set Up Or Run My Python IDE On VS Code On Windows 8 / How To Write A Guessing Game Program In Python 3 / Please Review My Price comparison engine (2) (3) (4)
My Python Guessing Game... Pythonistas Please Come In by phililp(m): 11:40am On May 11, 2017 |
dear pythonistas. a newbie in python. am trying to make a game that describes a fruit and the user try to guess the name of the fruit but am having som errors i dont understand.. here is my code import random my_dict = {"mango": "green when unripe and yellow when riped.. has one seed. hipogeal type of seed germnation", "apple": "look kinda sperical.. has many seeds. look green when unripe. and when riped; some looks red some yellow some still green"} print('I will describe the fruit and you will say its name') item_describtion = random.choice(my_dict.values()) chances=5 print ('you have 5 chance for the guess') print (item_describtion) while chances > 0: word = str(input( 'what is the name:')) chances -=1 if word == item_describtion.key(): print('you got it') break else: print (' try again') if chances == 0: print('sorry used ur five chaces') break print ('you have', chances, 'chance left') print ('the end') i know the error in comming from trying to get random.choice of the dictionary because i used a list implace of the dictionary and it ran well.. so the real probem is[b] how to get a random value from a dictionary[/b] and how to check if user's input is == key of the random item picked please help me acheive this.. thanks this is the error i get
|
Re: My Python Guessing Game... Pythonistas Please Come In by Nobody: 1:31pm On May 11, 2017 |
I Philip, I'm not a pro actually but I do understand how python works.... I noticed your dictionary, and your trying to random choose from it.. N.B a dictionary has a "key" and a "value ". You can only access a dictionary via its key. so it ought to be : Although you could make your description a key and your key a description.. But that isn't conventional and other programmers reading your code might have hard time. Bt for the main time I'll rather you use a tuple / a list for this kind of game you are trying to build. Because you intended this: print('I will describe the fruit and you will say its name') Having a parameter for your values using something like this y = (fruit = apple, description = " blah, blah blah " Then print description For description in y print:..... . then ask the player to enter the key If key = apple : then score += 1 else: score += 0 I could Ave done it n send it to you now but I'm in a bus travelling.. Hopefully when I get home I'll send a detailed version.. The things I wrote above are in pseudocode so just follow it.. if you don't get it still drop a reply but I post a detailed answer when I get home. There are interesting ways to draw up games like this . I hope I get time to show you how they work later on |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 4:20pm On May 11, 2017 |
kayoph: thanks a lot kayoph for taking ur time to help me... well i would like you to still give be details when you go home. but for the main time; i will try working with this ur pseudo code.. thanks meanwhile... save journeys |
Re: My Python Guessing Game... Pythonistas Please Come In by 4kings: 5:18pm On May 11, 2017 |
Use this to solve the error: item_describtion = random.choice(list(my_dict.values())) Note that the above code wraps the values to a list. 1 Like |
Re: My Python Guessing Game... Pythonistas Please Come In by greatface(m): 6:48pm On May 11, 2017 |
USE 4KINGS'S METHOD TO GET THE VALUES. AS AGAINST KAYOPH'S IDEA, YOU CAN GET THE VALUES USING DICT.VALUES(). JUST THINKING. |
Re: My Python Guessing Game... Pythonistas Please Come In by Nobody: 10:02pm On May 11, 2017 |
Philip.. I'm sorry I won't be able to send a screen shot this night.. I just got back to my area in Ogun, nd I heard that Nepa hasn't been our friend for weeks now..I mean my pc is down. anyways I've read through the comments and I can see suggestions about using the .value() : let me share what I think : 1) dictionary.values() (that's if you are using python 2 which I think it's obsolete for anyone to be using that now) 2.) list(dictionary.values()) ( that's in python 3) A dictionary in python works like a real dictionary in real life (the Longman, Oxford. etc.). Like I said earlier a dictionary has a key and a description. So you can only access the contents of a dictionary via its key just like you can search up the meaning of a word using the word-name in a dictionary. Now using the either of the ways I listed above is only going to give you a list of your dictionary.. let's take for example : dict = {"Name": "Philip", " Age": 7} d = {"Philip" : "A young boy of age seven "} Print(list(dict.values())) will give you : Value : ['7' 'Philip' ] ( or Value :['Philip' '7']... I'm not sure but either of the two will be produced I could have run it now but my pc can't rescue me now). Print(list(d.values())) will give you: Value : ['Philip' 'A young boy of age seven'] This simply gives you a return of view of the dictionary’s values instead ( I think a G mentioned it also) and I believe that's not what you are looking for... |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 10:52pm On May 11, 2017 |
kayoph: thanks.. i think with this i can solve may first problem.. but now how do i check to see if the users input is the key of the random selected value.. i.e if user_input == items_description's key print ('you got it) # where item_description = random.choice(list(my_dict.value())) please how do i implement this |
Re: My Python Guessing Game... Pythonistas Please Come In by 4kings: 4:19am On May 12, 2017 |
phililp:my_dict.values() returns a string and not a dictionary. Therefore using item_description.key() is also the same as doing 'string'.key() and key is not an attribute of a string. For example: >>> name = 'seun' >>>name.key() pythons return error message because 'key' is not an attribute of a string. So to solve your problem, do this instead: for fruit, details in my_dict.items(): I hope you know what items() does, so i don't need to explain that. If you don't you can quote me. |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 11:30am On May 12, 2017 |
4kings: yea i know the my_dictionary.items () returns the key and value of each item in the dictionary.. but i dont understand what the for fruit, details in ... does |
Re: My Python Guessing Game... Pythonistas Please Come In by 4kings: 12:53pm On May 12, 2017 |
phililp:They're just loop variables that are useful for iteration. >>>for x in range(3): #the result will be 0, 1, 2 x is the variable in this loop iteration. You know that range of 3 is 0,1 and 2 Therefore x represent each value in the range in the course of the iteration. However a dictionary(or the dictionary items) is like a tuple of two values: ('keys' and 'values') and not made of just a single value like the range. So i used two loop variables for the dictionary to represent the keys and values which in this context are your fruits and description I hope i explained this well. |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 9:20pm On May 12, 2017 |
4kings: crystal clear sir.. i will build on it. |
Re: My Python Guessing Game... Pythonistas Please Come In by silento(m): 10:57pm On May 12, 2017 |
here is the solution not values() but. keys() |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 11:28pm On May 12, 2017 |
silento: bruv! is this some kinda joke?? |
Re: My Python Guessing Game... Pythonistas Please Come In by fleshbone(m): 11:57pm On May 12, 2017 |
silento is right bro!!! Here's what i did with my 2.7 though and it worked perfectly. go through it and i hope you get the best out of it. import random from time import time,sleep my_dict = dict() my_dict['mango'] = '''green when unripe and yellow when riped.. has one seed. hipogeal type seed germination''' my_dict['apple'] = '''Look kinda speriacl... has many seeds. looks green when unripe; some looks red some yellow some still green''' print('I will describe the fruit and you will say its name') item_description = random.choice(my_dict.keys()) sleep(1) print(my_dict[item_description]) chances = 5 my_guess ='' while True: ---if chances != 0: ------print('Enter your Guess') ------user_guess = (raw_input()) ------if user_guess == item_description: ---------print ('correct! you only used', 6 - chances, 'chance') ---------break ------else: ---------chances -= 1 ---------print ('Sorry, try again. You have', chances, 'chances left') ---else: ------print('Sorry, you\'ve run out of chances to guess. yor have', 0, 'guesses left') ------break ---------------------------------------------------------------------------------------------------------------- best of luck phililp boss meanwhile, dont forget that when you have a chance left, it should be "1 chances left". |
Re: My Python Guessing Game... Pythonistas Please Come In by silento(m): 2:55am On May 13, 2017 |
phililp: do I look like a joker to you the fix to the error in you code the time I was learning python I figured every thing out my self do you want to test what you have learnt so far build a gui lover calculator tkinter for gui flame game type goodluck |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 12:32pm On May 13, 2017 |
silento: thanks bro! i will try it.. started learning three weeks ago, dont know about GUI yet... but with time i definitely will.. thanks for the hint... |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 12:38pm On May 13, 2017 |
fleshbone: thanks sir... but this wont produce the required result.. because .. item_description = random.choice(my_dict.key()) so if you print item_description the output would be a random selected key... not its value dunno what the sleep does though.. |
Re: My Python Guessing Game... Pythonistas Please Come In by fleshbone(m): 1:48pm On May 13, 2017 |
phililp: Oh agreed! That's why after the sleep (to cause a delay in seconds) The print(my_dict[item_description]) come to display the value of the random key. Study the codes carefully again bro Phililp |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 12:17am On May 14, 2017 |
fleshbone: i have sir.. i think maybe u dont seem to understand the question... this is it.... each key in the dictionary is the name of the fruit...and the value of the key is the description of the fruit..so now.. item_describtion = random.choice(my_dict.values())... #this will return a random value(description of any randomly selected fruit) print (item_description) #prints the returned value/description and not the key.. then now in the while loop... we wana check to see if the user's input is same as the key of that printed value/description so if u say... if user_input == item description: # this is wrong because it will rather check to see if user's input is == item_description# which was = a random selected value and not key.... but our aim is to check if the user's input is the key to the printed value... so i dont know how to implement this.. i tried using if user_input == item_describtion.key()# but this will give an attribut error cos key is not an attribute of a dictionary ... but keys() is.. which implies the whole keys in that dictionary.. but if we again say: if user_input in my_dict : # this will run well but cause a (the hell do you call that error again..) .. yas!! SEMANTIC ERROR.. because say the description of apple was printed and the user inputs mango .... it will accept as tru cos mango is in my_dict. hope understand ... so now thats my pain in the butt.. how to check if the user's input is == the key of that very printed description.. .. thanks to the house BTW.. am enjoying this thread. |
Re: My Python Guessing Game... Pythonistas Please Come In by fleshbone(m): 12:19pm On May 14, 2017 |
phililp: Hahahahahaha. Calm down bro! Yes.! Key is kind of the name and value the data associated to key. In PHP the dict() is called an associative array. The easiest way out as for me, is to get the random key once. Say 'Key = random.choice(my_dictionary.keys())' This ('Key') we understand would hold a random key from the dictionary. If you agree with the above, then to display the description associated with this Key 'Value = my_dictionary[str(Key)]' Note: Very important you convert the key to a string where Key is the random key we initialized above. Print (Value) 'User_input = input()' If you get some error with above then use 'User_input = raw_input()' For the test, 'If user_input == Key:' I seriously my explaination is good enough I'll be waiting Phililp |
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 8:09pm On May 14, 2017 |
is it the weed i took last week that is still disturbing me or the fish brain section of my brain just climaxed.. lol! thanks bro.. but how come i cudint figure awt somthing as this so soon.... anyway thanks once again.... u just got me a certificate of GO AHEAD.. once again thanks to all of ya... i have also learnt more things from each comments... |
Re: My Python Guessing Game... Pythonistas Please Come In by sharrp: 11:37pm On May 14, 2017 |
The problem is using random |
Re: My Python Guessing Game... Pythonistas Please Come In by sharrp: 11:37pm On May 14, 2017 |
The problem is using random, dictionaries are not number indexed like arrays. |
Re: My Python Guessing Game... Pythonistas Please Come In by fleshbone(m): 12:08pm On May 15, 2017 |
phililp: Lols! U're always welcome. Am glad I was useful. |
Re: My Python Guessing Game... Pythonistas Please Come In by adejumoadeoluwa(m): 4:38pm On May 15, 2017 |
if you are interested in joining a Python Fellowship. then .... https:///KTkRJuHrxs36Zq5r5u6DAW |
Re: My Python Guessing Game... Pythonistas Please Come In by Nobody: 12:15am On May 16, 2017 |
phililp: Philip, sorry i was just able to get my Pc on this night.... i Used python IDLE You might have fixed the problem some other way but I hope this clarifies my point. You should see the accessing of the dictionary using the: "if word in my_dict and my_dict[word] == item_describtion " i accessed the dictionary using the key. The key is the requested input.
|
Re: My Python Guessing Game... Pythonistas Please Come In by phililp(m): 8:24am On May 16, 2017 |
kayoph: thanks man.. this also worked well... i appreciate ur support and concern 1 Like |
Re: My Python Guessing Game... Pythonistas Please Come In by severus(m): 12:45pm On May 16, 2017 |
Do you have coding skills any of the following (Java, C#, Unity, Angular js), knowledge sharing and a desire to build your own thing and make an impact?.. why not hit me up, let us collaborate and build a startup together! Please this is ONLY for someone who wants to be an entrepreneur. 08166459353 whatsapp or ndukaude@gmail.com. Cheers 1 Like |
(1) (Reply)
How Important Is CPN Certification (computer Professional Of Nigeria) / Web Development Vs Software Development. Which Is Better In Nigeria? / Why Most Of 9ja Programmers Are Broke
Viewing this topic: 1 guest(s)
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 83 |