Python Forum
Troubles with program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with program
#1
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.
Reply
#2
you define, but never use letter
Reply
#3
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.
Reply
#4
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.
Reply
#5
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?
Reply
#6
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.
Reply
#7
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.
Reply
#8
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.
Reply
#9
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 :/
Reply
#10
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling a list troubles giveen 7 3,967 Jan-11-2019, 08:05 PM
Last Post: giveen
  Troubles on how to use Open CV knowledge1st 1 2,501 May-23-2018, 05:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020