Python Forum
Troubles with program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Troubles with program (/thread-27155.html)

Pages: 1 2 3 4


Troubles with program - Harambe - May-27-2020

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.


RE: Troubles with program - Larz60+ - May-27-2020

you define, but never use letter


RE: Troubles with program - Harambe - May-27-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.


RE: Troubles with program - Larz60+ - May-27-2020

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.


RE: Troubles with program - Harambe - May-27-2020

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?


RE: Troubles with program - deanhystad - May-28-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.


RE: Troubles with program - perfringo - May-28-2020

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).


RE: Troubles with program - Knight18 - May-28-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.


RE: Troubles with program - Harambe - May-28-2020

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 :/


RE: Troubles with program - buran - May-28-2020

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