Hi, i was working on a code for a vending machine and i do consider myself to be a beginner, anyway i feel my code is much longer than it needs to be and i want someone to show me a new, shorter way of presenting it.
If you don't have Python 3.4 or later you'll have to make your own homegrown Enum class, or use something like def enum (.sequential,.named): enums = dict (zip (sequential, range (1, len (sequential) + 1)),.named) return type ('Enum', , enums) Reel = enum ('CHERRY', 'LEMON', 'ORANGE', 'PLUM', 'BELL', 'BAR', 'SEVEN'). Machine learning algorithms. They also achieved the first superhuman pat. Right-side: the fruit after the background removal and after it was. Scaled down to 100x100 pixels.
Thanks.
- 5 Contributors
- forum5 Replies
- 12,867 Views
- 2 Years Discussion Span
- commentLatest PostLatest Postby Reverend Jim
Recommended Answers
Fruita, fruitb, fruitc = allfruit I think single letter variable names (excluding and loop counters) should be avoided at all costs. You currently have all the fruits values hard coded in, it might be worth while to extract that data into a file to make it easier to add/remove new ones.
Before making any suggestions, I would like to ask if your course of study (whether personal or formal) has gotten to functions and how to write them yet. This would be a big factor in how you approach thsis matter.
Jump to Post
The first thing is to never have variable names containing a variable index, such as product_1, product_2,
etc. Use lists or tuples instead
Then product[0]
is …
Jump to Post
All 5 Replies
Schol-R-LEA1,117Commie Mutant Traitor Featured Poster
Before making any suggestions, I would like to ask if your course of study (whether personal or formal) has gotten to functions and how to write them yet. This would be a big factor in how you approach thsis matter.
Editedby Schol-R-LEA
In this challenge we are going to write a Python program that automatically generates six random numbers (from 1 to 50) and display them on the screen, sorted in ascending order.
The program will need to make sure that each number is unique; the same number cannot come twice in the selection of six selected numbers.
Learning Objectives
By completing this code, you will understand the difference between the two types of loops used in Python:
- For Loops (Count-Controlled Loops): A count-controlled loop is one that is executed a certain number of times. In our example, because we know that we need exactly six numbers we are going to use a for loop to repeat (iterate through) some of our code exactly six times.
- While Loops (Condition-Controlled Loops): A condition-controlled loop such as a While loop keeps going while a certain condition is true. As soon as the condition becomes false, the loop stops iterating. In our case, when generating a random number, we are going the check that the new number has not already been picked up. If it has then we are going to generate a new one and will carry on doing so till we get a number that has not already been picked up.
Check the code
Your Challenge
- Complete this code to store your own selection of six numbers. For testing purposes, you can store these numbers directly into the code and display these to the end-user. Alternatively you can ask the end-user to type six numbers.
- Then your code will use the program given above to generate the six lottery numbers.
- Finally your code will check and inform the end-user of how many numbers the user guessed correctly by comparing the user’s six numbers with the six randomly generated lottery numbers!
Note
The probability of guessing several lottery numbers is very low. You may want to tweak your program to only pick up numbers between 1 and 20 (instead of 50). This will give you a better chance to guess some of these numbers and hence test whether your code is working.
Video Tutorial
Fruit Machine Python Download
Other challenges you may enjoy...
Fruit Machine Python Tutorial
Tagged with: iteration, List, Python Challenge