The Student Room Group

FREE Programming/Coding Help Desk

Scroll to see replies

Original post by bigmansouf
problem.jpg

I have a problem as i am a newbie. I am learning how to code python from the book by dierbach- intro to comp sci using python.But i am confused on how the author thought about the code. The author did not say why.
If you look at the diagram i attached you will see that the author says something like: "if the month is May, then add 2 to value, else".
My question is why add 2 and not 5 or 8.
I wnat to know why he increments the value by random numbers.

Please explain this to me as I dont understand.

Thank you very much for reading and help


See how you have the first five steps, could you give us the extra steps after that too? We won't know why unless we know what he is going to do with value.

Also, I have a feeling this will be more mathematical than programming (I'm assuming you want to know how he gets the day of the week from the date).

EDIT: I think because if the first day of January was a Monday (give that a value of 0) then the first day of May will be Wednesday (so add two onto it). For example, Friday was the first day of January this year and Sunday was the first day of May, so he added 2 to the value. I guess he will do the mod function later on somewhere (at the end) to finally get the day of the week.
(edited 7 years ago)
Original post by CheetahCurtis
See how you have the first five steps, could you give us the extra steps after that too? We won't know why unless we know what he is going to do with value.

Also, I have a feeling this will be more mathematical than programming (I'm assuming you want to know how he gets the day of the week from the date).

EDIT: I think because if the first day of January was a Monday (give that a value of 0) then the first day of May will be Wednesday (so add two onto it). For example, Friday was the first day of January this year and Sunday was the first day of May, so he added 2 to the value. I guess he will do the mod function later on somewhere (at the end) to finally get the day of the week.

ok thanks
Hey!

How do you generate a set of random numbers and allocate it to the user on python. Like say if they input the question 'How do i fix my computer?' , they should be given a unique id from a set of random numbers and yeah they will be basically given a case number which will then be sent off to a technician.

How would I code this with python?

Thanks!
Reply 643
Original post by bluepearl7
Hey!

How do you generate a set of random numbers and allocate it to the user on python. Like say if they input the question 'How do i fix my computer?' , they should be given a unique id from a set of random numbers and yeah they will be basically given a case number which will then be sent off to a technician.

How would I code this with python?

Thanks!


Just use a GUID. You won't even need to check for duplicates, as a 128 bit GUID is pretty much guaranteed to be unique (can look up maths on it if interested). So you generate it, store it in the db against the user, and lastly display it to the user.
How do I implement in a class in Python to buy 10 stocks and then sell 10 stocks over a given timeframe? I'm not sure if I need to use an algorithm or anything. Can anyone how me an example please?Thanks!
(edited 7 years ago)
Reply 645
Original post by bigmansouf
Q: Write a function called pattern_sum that receives two single digit positive integers, (k and m) as parameters and calculates and returns the total sum as:
k + kk + kkk + .... (the last number in the sequence should have m digits)
For example, if the two integers are:

(4, 5)
Your function should return the total sum of:
4 + 44 + 444 + 4444 + 44444
Notice the last number in this sequence has 5 digits. The return value should be:
49380


Iam trying to use Lambda to solve this question

please help def two(a, b):

total = list(map(lambda i: int(str(a) * i), range(1, b+1)))
return total[-1]
two(4, 5))
this is what i have so far

i would like to use it with out having to splice a list
is there a way?


Actually can just do it with a single multiplication.

4 + 44 + 444 + 4444 + 44444 == 4 * 12345

As it's two single-digit positive integers, you can just store the needed sequence (0,1...123456789) in an array.
(edited 7 years ago)
How do I programme a website in PHP. Need help urgent
Original post by PrincessZara
How do I programme a website in PHP. Need help urgent


Learn PHP
Reply 648
Original post by PrincessZara
How do I programme a website in PHP. Need help urgent


Need to be more specific!
I am currently doing conouting and coding in Visual Basic, however im struggling with arrays. The question is: Write a program which has two arrays. One array contains a list of questions, and the other contains the corresponding answers. Your program should randomly select questions from the question array, allow the user to enter their answer, and then check this using the answer array.
Any advice on how to create the arrays, how to input the questions and answers and how to make sure the answers are with the questions
Thank you!!!
Original post by Rhiannawintrip
I am currently doing conouting and coding in Visual Basic, however im struggling with arrays. The question is: Write a program which has two arrays. One array contains a list of questions, and the other contains the corresponding answers. Your program should randomly select questions from the question array, allow the user to enter their answer, and then check this using the answer array.
Any advice on how to create the arrays, how to input the questions and answers and how to make sure the answers are with the questions
Thank you!!!


I haven't done everything you've wanted. Run the code and you can fill out the blanks in your requirements. I've just given you a good start.

Spoiler

(edited 7 years ago)
hi, as part of my A Level coursework i think i am planning on designing a booking system for my mums and dads work. so would it be possible if i created a microsoft database and then imported it to a microsoft website which i will be building using a microsoft website software?

thank you!
Hi,

How do I make make python prompt user to enter an valid input if they enter blank space,punctuation or integer?
The user should be prompted to enter a valid response like 5 times and if they don't the program should exit. But I'm struggling to d this as I am only a beginner.

E.g
resolved=("Is your issue solved?")
if resolved..

Thanks
Original post by bluepearl7
Hi,

How do I make make python prompt user to enter an valid input if they enter blank space,punctuation or integer?
The user should be prompted to enter a valid response like 5 times and if they don't the program should exit. But I'm struggling to d this as I am only a beginner.

E.g
resolved=("Is your issue solved?":wink:
if resolved..

Thanks


I already gave you an answer. It's your coursework, please stop expecting people to do it all for you. I gave you more then enough help to get you by, if you can't figure out the rest then I strongly encourage you to drop the subject.
Original post by bluepearl7
Hi,

How do I make make python prompt user to enter an valid input if they enter blank space,punctuation or integer?
The user should be prompted to enter a valid response like 5 times and if they don't the program should exit. But I'm struggling to d this as I am only a beginner.

E.g
resolved=("Is your issue solved?":wink:
if resolved..

Thanks


Here you go.

EDIT: I used semi-colons but they are not necessary when coding in Python. I learn other languages and so I have grown a habit of using it.

EDIT: This forum translates my code differently from how I have written it and so there may be minor errors - easy fix though!

counter = 0;

loop = True;

while loop == True:

response = input("");

punctuations = [""]; # insert punctuations here

if response == int or " " or punctuations:
counter = counter + 1;
print("ERROR.");
print(counter);

if counter >= 5: # you can use if statements within if statements (nesting is thing, ya know)
loop = False; # you were not explicit enough on how you want to end the program but this is where you tell python on how you want to end the program
print("END.");

else:
print(); # i like to add this if else statements are empty but it's not necessary
(edited 7 years ago)
Original post by s4b3rt00th
Here you go.

EDIT: I used semi-colons but they are not necessary when coding in Python. I learn other languages and so I have grown a habit of using it.

EDIT: This forum translates my code differently from how I have written it and so there may be minor errors - easy fix though!

counter = 0;

loop = True;

while loop == True:

response = input("":wink:;

punctuations = [""]; # insert punctuations here

if response == int or " " or punctuations:
counter = counter + 1;
print("ERROR.":wink:;
print(counter);

if counter >= 5: # you can use if statements within if statements (nesting is thing, ya know)
loop = False; # you were not explicit enough on how you want to end the program but this is where you tell python on how you want to end the program
print("END.":wink:;

else:
print(); # i like to add this if else statements are empty but it's not necessary


Would it be better to write
counter += 1instead of counter = counter + 1
Reply 656
Original post by thejj100100
Would it be better to writecounter += 1instead of counter = counter + 1


it doesn't really matter as long as you understand what either mean. Most use the first way because it's shorter to type.
First two lines of a given pseudocode are:for a = 8 to 19 step 4

for b = 1 to 3

Im guessing that this means that "a" is in the range of 8 to 19, and "b" is in the range of 1-3, is this correct?If so, what does the "for" mean at the start if both lines? And what does "step 4 mean"? The pseudocode is about printing different words depending on whether a calculated number (dividing one of them by the other and looking at the remainder) isodd/even/positive/negative, and printing an output based on that condition.

Also though, how would you do something like this, because when you divide "a" by "b", "a" and "b" aren't defined integers, they are a range of numbers (I think, so "a" is 8 to 19, "b" is 1 to 3 if I understood this correctly). I could do it and follow it if "a" and "b" had a defined number, but I think they have multiple numbers (if I am right), so how could you say to divide a by b when both a and b have different possible integer values?

Thanks
(edited 7 years ago)
Original post by s4b3rt00th
Here you go.

EDIT: I used semi-colons but they are not necessary when coding in Python. I learn other languages and so I have grown a habit of using it.

EDIT: This forum translates my code differently from how I have written it and so there may be minor errors - easy fix though!

counter = 0;

loop = True;

while loop == True:

response = input("":wink:;

punctuations = [""]; # insert punctuations here

if response == int or " " or punctuations:
counter = counter + 1;
print("ERROR.":wink:;
print(counter);

if counter >= 5: # you can use if statements within if statements (nesting is thing, ya know)
loop = False; # you were not explicit enough on how you want to end the program but this is where you tell python on how you want to end the program
print("END.":wink:;

else:
print(); # i like to add this if else statements are empty but it's not necessary


Absolutely terrible and inefficient way of doing this. Look up on the Regular Expressions library please.
Reply 659
Original post by Aklaol
Absolutely terrible and inefficient way of doing this. Look up on the Regular Expressions library please.


The person who asked the question clearly stated that he was a beginner. What makes you think it's appropriate to introduce regular expressions to someone who has not yet grasped the basic concepts of loops and selection statements?

Nothing is terrible about his code and I fail to understand why it would be inefficient too, it's not exactly computationally slower than a regular expression query.

Quick Reply

Latest

Trending

Trending