Posts: 16
Threads: 2
Joined: May 2020
May-27-2020, 10:38 PM
(This post was last modified: May-27-2020, 11:06 PM by Larz60+.)
Hello!
I am a student and I am learning for my admission test to the university and I was wondering if there is any possibility to create any “light” program to test myself before the exam day. The problem is I am really bad at programming at all and I have never been doing in Python.
The program should be like: Generate 100 combinations of Number (1-1500) + 4 letters (a-h). Everything should be random but in a row (for example: 3acgh, 7efgh, 9cdeg is right, but 7hcea, 10aebh, 2beac is incorrect). Is here any possibility to make a program like this for newbie like me? I did some research on internet and did this:
import random
number=random.randrange(1500)+1
letter=[a,b,c,d,e,f,g,h]
for i in range(100):
print(number) Actually, I don't know what I did and if it is even correct.
Thank you for any help.
Posts: 11,873
Threads: 474
Joined: Sep 2016
you define, but never use letter
Posts: 16
Threads: 2
Joined: May 2020
Yes, cause I really dont know what to do next. It is just a laic research from youtube and internet. Honestly, I dont even know what did I write.
Posts: 11,873
Threads: 474
Joined: Sep 2016
May-27-2020, 11:17 PM
(This post was last modified: May-27-2020, 11:18 PM by Larz60+.)
print statements will always help:
import random
number=random.randrange(1500)+1
print(f"number: {number}")
letter=['a','b','c','d','e','f','g','h']
for i in range(100):
print(number) outout:
Output: 850
850
850
... # 101 times
So:
alpha literals must be enclosed in single or double quotes
you create a random number in line 3, and then proceed to print it 101 times.
once on line 4
and then 100 times on lines 7 & 8
the only number that varies is 'i', but you never use it.
Posts: 16
Threads: 2
Joined: May 2020
May-27-2020, 11:25 PM
(This post was last modified: May-27-2020, 11:25 PM by Harambe.)
import random
number=random.randrange(1500)+1
print(f"number: {number}")
letter=['a','b','c','d','e','f','g','h']
for i in range(100):
number=random.randrange(1500)+1
print(number) What about this?
Posts: 6,221
Threads: 16
Joined: Feb 2020
You are starting out all wrong. How would you solve this problem without a computer? If you cannot describe HOW to solve the problem you cannot write a program to solve the problem. With a pencil and piece of paper yow would you generate 10 random sequences and put them in order? If you cannot figure that out try solving the problem of creating one random sequence. If you can do that, generate two random sequences and put them in the correct order.
Posts: 1,936
Threads: 8
Joined: Jun 2018
You should have a 'plan' (or 'algorithm') to solve the problem. First in spoken language, then translated into Python.
1. Get random number from range
2. Get random 4 letters (in sorted order?)
3. Combine them together
There is no clarity, whether result should be sorted or only letters part.
Keep eye on datatypes while combining (number is integer and letters are string).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 60
Threads: 9
Joined: May 2020
Start small. Break the task into smaller pieces, focusing on one problem at a time. Keep expanding from that point. Try to do all of it at once can be a bit overwhelming.
Posts: 16
Threads: 2
Joined: May 2020
May-28-2020, 09:25 AM
(This post was last modified: May-28-2020, 09:26 AM by Harambe.)
import random
import string
number=random.randrange(1500)+1
letter=random.choice(['a','b','c','d','e','f','g','h'])
for i in range(100):
number=random.randrange(1500)+1
letter=random.choice(['a','b','c','d','e','f','g','h'])
print(number)
print(letter) Now it makes me something like this:
Output:
NUMBER
LETTER
NUMBER
LETTER
....
I cant find out how to stick them together and generate 4 letters and without repeating :/
Posts: 8,090
Threads: 154
Joined: Sep 2016
take 4 letters - either in loop one by one or using random .sample. Join them. Take one random number ( randrange(1, 1501) or randint(1, 1500) ). Join the number and the letters
|