Sharing variables between functions? (Python3) - function

Here is the code I have written in Python:
def checkout_item(item):
quant = int(input('Enter the quantity of the item purchased: '))
price = float(input('Enter price of item: $'))
item_total = quant*price*1.00
print(f'Your item total is: ${item_total}')
return item_total
def main():
count = 1
order_total = 00.00
flageus_maximus = True
while flageus_maximus:
order_total+=checkout_item(count)
count+=1
print(f'Item no: {count}')
keep_going = input('Do you have more items to checkout?')
if keep_going!='yes':
flageus_maximus=False
print(f'Your order total is ${order_total}')
main()
I want to print 'Item no:' before item_total, but in order to do that, I have to readjust my checkout_item function. I was thinking of returning the count variable through the checkout_item(item) function into the mainline, but the checkout_item(item) function already returns item_total. What can I do?
I tried to set a count variable in checkout_item(item), by adding a variable count_checkout = 0 in checkout_item(item), but I got a message stating count_checkout: Unbound. I'm guessing it is because I cannot return two variables from checkout_item(item) as I call it in the main()

Related

Average depending of parameter given

I have a month parameter called DimTiempoMes So I when I run preview of report I select that month in combo box it can be from 1 to 12.
If I select 1 it return value of Fields!InventarioDirecto_Monto.Value of month 1
If I select 3 it return value of Fields!InventarioDirecto_Monto.Value of month 1,2 and 3 etc...
Now I want to get Average depending of selected Fields!InventarioDirecto_Monto.Value
For Example: If I selectDimTiempoMes with value10, formula will be for example:
InventarioDirecto_Monto Value / DimTiempoMes.Value
Execution: Sum(1,2,3,4,5,6,7,8,9,10) / 10
So I try next expression:
=SUM(Fields!InventarioDirecto_Monto.Value) / CINT(replace(left(Split(Parameters!DimTiempoMes.Value,"[").GetValue(3),2),"]",""))
Problem is SUM(Fields!InventarioDirecto_Monto.Value)always come with sum of 12 months values, so it always sum values of 12 months
Question is, How can I get only values that Fields!InventarioDirecto_Monto.Value return depending of selected parameter DimTiempoMes?
Some Data:
In this image I have 3,719 in first column because I use DimTiempoMes as 1: so my colum Acum who have expression should be 3,719 too because SUM(Fields!InventarioDirecto_Monto.Value) equals to 3719 and divisor equals to 1 because I search 1. So 3,719/1 = 3791 and I getting 40,593
Other Example: if I use DimTiempoMes as 4
There I should get 3719 + 3639 + 3378 + 2999 / 4 = 3433.73 and I receive 10148
Update:
I also try adding custom code like:
Function Promedio(ByVal items As Object(),ByVal meses As Object(), ByVal Valor1 As integer) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
Dim ct as Integer = New Integer()
suma = 0
For Each mes As Object In meses
IF CINT(mes) <= Valor1
Then ct = 1
For Each item As Object In items
If (ct = CINT(mes))
Then suma += Convert.ToDecimal(item)
End If
ct += 1
Next
End If
Next
suma = suma/valor1
If (Valor1 = 0) Then return 0 else return suma
End Function
And Expression something like:
=Code.Promedio(LookupSet(Fields!Dim_ConceptosInventario_.Value, Fields!Dim_ConceptosInventario_.Value, Fields!InventarioDirecto_Monto.Value, "DS_SubInventarioDirecto"),LookupSet(Fields!Dim_ConceptosInventario_.Value, Fields!Dim_ConceptosInventario_.Value, Fields!nPeriodo.Value, "DS_SubInventarioDirecto"),CINT(replace(left(Split(Parameters!DimTiempoMes.Value,"[").GetValue(3),2),"]","")) )
But I donĀ“t get desire results

How to Count Enum Across Table MYSQL?

I have a table studentexamrecord
Name|Module1|Module2|Module3|Total
A-------RT-----PASS---PASS
B-------RC-----RC------RT
C-------RT-----PASS----RC
I would like to calculate the total number of column that contains RT and RC at the total column.
What should i do?
Please give it a try:
SELECT
Name,
Module1,
Module2,
Module3,
(
(Module1 = 'RT' OR Module1 = 'RC')+
(Module2 = 'RT' OR Module2 = 'RC')+
(Module3 = 'RT' OR Module3 = 'RC')
) AS Total
FROM your_table
Note: MySQL boolean expression resolves into 0/1. Let's say if Module1 is RC or RT then the following expression returns 1
(Module1 = 'RT' OR Module1 = 'RC') returns 1
otherwise it returns zero (0)
This query returns the sum of total in record that contain module1 = RC edit it to much you condition :
Select
sum(if(module1 = RT, total, 0))
from
studentexamrecord;

GroovySql - retrieving list of values for named parameter to use with sql in operator

I'm trying to figure out the correct syntax to use to pass a list of values to the 'in' operator using GroovySql. When I pass in a comma separated list of values only the first value gets applied while the other values are ignored.
My code looks like:
final String query = '''\
SELECT
li_ch.event_id as event_id,
sum(CASE li_ch.line_item_type WHEN 0 THEN 1 WHEN 900 THEN (CASE li_p.line_item_type WHEN 0 THEN -1 ELSE 0 END) ELSE 0 END) sold,
sum(li_ch.amount) as gross
FROM
line_item li_ch LEFT JOIN line_item li_p on li_ch.parent_line_item_id = li_p.id,
sale s
WHERE
s.id = li_ch.sale_id AND
li_ch.event_id in (${Sql) AND
s.sale_status_type in (:eventIds) AND
li_ch.line_item_type in (0, 900)
GROUP BY li_ch.event_id
'''
// Create new Groovy SQL instance with injected DataSource
final Sql sql = new Sql(dataSource)
def ids = eventList.collect { "'$it'" }.join(",")
final List<GroovyRowResult> rows = sql.rows(query, eventIds: Sql.expand(ids))
rows
There's a pull request to make this easier, but for now you have to join your own attributes together:
def values = "'${list.join('\',\'')}'"
// or less confusingly:
// def values = list.collect { "'$it'" }.join(",")
sql.rows( "select * from table where value in (${Sql.expand(values)})" )

How to assign Rank in SSRS report?

I have a column (impact/hr) which gets populated after some calculations. Now I would like to assign each row a rank. For example Maximum dollar amt gets the rank of 1. Any tips to write an expression would be appreciated. Thank you.
Just add some custom code in Report-Report Properties...-Code:
Dim Rank AS Integer
Function GetRank() AS Integer
Rank = Rank + 1
Return Rank
End Function
In your tablix's detail cell where you want the rank, use the following formula:
=Code.GetRank()
Each time it processes that cell, it adds one to the rank and displays the result.
It may be your secondary option !
You can arrange the Impact\Hr in ascending order and the show the Rank by using RowNumber function.
For More info see the RowNumber function.
Use the Rank function.
select [Impact per Hr], [My Rank] = Rank() over (order by [Impact per Hr] desc)
then use asc/desc depending on which value you want to be ranked high or low.
This is independent of any order by statement as well
Rank() is preferable to Row_Number() for applications like this because rows of the same value will have the same Rank, but will have a different Row_Number.
Step 1: Add the following code in code section of reports properties in Reports Builder.
Public dim previousValue as Integer = 0
Public dim i as Integer = 0
Public dim flag as Boolean = False
Public dim lastRank as Integer = 1
Public Function Rank(sourceValue as Integer ) As Integer
if previousValue = sourceValue
i = i + 1
flag = True
end if
if previousValue > sourceValue
lastRank = lastRank + 1
flag = False
end if
previousValue =sourceValue
if flag = True
return lastRank
else
lastRank = lastRank + i
i = 0
return lastRank
end if
End Function
Step 2: Call the above function from the cell where you want to display rank like this:
=code.Rank(SUM(Fields!SumTotal_Ending_Balance.Value))

Linq-select group by & count

A controller should return data that is used as source for a D3.js line chart. In the controller, I've got a list of objects and I'm only interested in the property "Begin" and count(Begin). Begin is a datetime and what I want is to group all Objects where "Begin" is on the same day, and then count the number for each day.
I try to select this information and return it this way:
var results = from a in db.Questionaires
group a by a.Begin.Date into g
select new { Date = g.Key, Count = g.Count() };
return Json( results, JsonRequestBehavior.AllowGet);
Unfortunatly, I'm getting an error because the group by clause seems to be wrong ("The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.").
How to select the information the right way? If anybody has some examples on D3.js and MVC I would appreciate that.
You should try this
var results = from a in db.Questionaires
group a by new { y = a.Begin.Year, m = a.Begin.Month, d = a.Begin.Day}
into g
select new { Day = g.Key.d, Year = g.Key.y,
Month = g.Key.m, Count = g.Count(),
Date = g.Select(d=>d.Begin).FirstOrDefault() };