Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help.
#1
Hi I am a student,since now course include if statement,loops and list. assignment only allow course content.I am struggling for logic pls help me whats best shortest way to find out solution I tried for and while loop but without if statement i am unable to do coding.I know its bit demanding,but I cant get logic at all if some one can help here.thanks in advance.
Write a function called earthquack, this function has 2 arguments. The first is called speed and the second is called units.
The function returns a list containing 2 strings.
The first element of the returned list is a feedback string, which is
one of the following:
‘Inputs are all valid’,
‘Speed not a positive whole number’,
‘The units must be either miles or kilometres’,
‘Both inputs are not valid’.
The second element of the returned list is an empty string if the input
is not valid, otherwise it is one of the following:
‘Not a hurricane’,
‘Category 1 - Very dangerous winds will produce some damage.’ ,
‘Category 2 - Extremely dangerous winds will cause extensive damage.’,
‘Category 3 - Devastating damage will occur.’,
‘Category 4 - Catastrophic damage will occur.’,
‘Category 5 - Catastrophic damage will occur.’
Use lists to store as much of the information as possible. This will
mean you can reduce the number of conditional statements. The simpler your program, the more marks you will receive. If your function
yields the incorrect result for any input, you will get zero marks.
Full marks will only be given for a function that does not use any if
statements. This means that your program uses only indicies of lists
to access the relevant information. Hint, to achieve this you can cast
a boolean to integer, i.e. int(True) is 1 and int(False) is 0.
To find the correct category you can use a while loop.
Examples: (Note quotation marks need to be replaced if you wish to
Reply
#2
I have to say that I think this is a really dumb exercise, but that's not your fault.

Here's what I think he is getting at when he talks about using lists:

x = 5
print(['x is not five', 'x is five'][x == 5])
On the second line, x == 5 resolves to True. But that is in brackets, so Python sees that as the index of the previous list (['x is not five', 'x is five']). So Python tries to convert True to an integer. True actually is an integer, and it's value is 1. So the print statement is equivalent to print(['x is not five', 'x is five'][1]), which prints 'x is five'. That's how you convert a conditional into a list. The general form is that:

if condition:
    true_result
else:
    false_result
is equivalent to:

[false_result, true_result][condition]
This is generally considered a bad way to code the conditional. In fact, the ternary expression was added to Python specifically to avoid this sort of construction.

To expand on True/False as integers a bit, you can combine two conditions using math. 2 * condition1 + condition2 is equal to:
  • 0 if neither condition is true
  • 1 if condition1 is false and condition2 is true
  • 2 if condition1 is true and condition2 is false
  • 3 if both conditions are true

He also mentions while loops. I expect this is the sort of thing he is talking about:

powers = [1, 10, 100, 1000]
index = 0
while x < powers[index]:
    max_value = powers[index]
    index += 1
print('x is no more than', max_value)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
that's lot help thanks you very much for your time and support.
Reply


Forum Jump:

User Panel Messages

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