Why am I getting a 0 at the beginning of my output? - python-3.10

person_name = ''
person_age = 0
person_name = str(input(person_name))
person_age = int(input(person_age))
print('In 5 years', person_name, 'will be', person_age + 5)
My output always has a zero at the beginning and I can't figure out why.

The input() function in python can take an optional argument input(prompt)
where prompt is a String, representing a default message before the input.
For your code you can just write
person_name = str(input())
person_age = int(input())
print('In 5 years', person_name, 'will be', person_age + 5)
Or
person_name = str(input('Enter your name:'))
person_age = int(input('Enter your age:'))
print('In 5 years', person_name, 'will be', person_age + 5)
Source

You're getting 0 at the beginning of the output because person_age is set to 0, and you pass it as the prompt message to input().
If person_name was not an empty string, you would see that one first.
The correct code is this:
person_name = ''
person_age = 0
person_name = input("Enter name: ")
person_age = int(input("Enter age: "))
print('In 5 years', person_name, 'will be', person_age + 5)

Related

Strings: How to combine first and last names in a long text string with other words?

Problem:
I have a given string = "Hello #User Name and hello again #Full Name and this works"
Desired output: = ["#User Name, #Full Name"]
Code I have in Swift:
let commentString = "Hello #User Name and hello again #Full Name and this works"
let words = commentString.components(separatedBy: " ")
let mentionQuery = "#"
for word in words.filter({ $0.hasPrefix(mentionQuery) }) {
print(word) = prints out each single name word "#User" and "#Full"
}
Trying this:
if words.filter({ $0.hasPrefix(mentionQuery) }).isNotEmpty {
print(words) ["Hello", "#User", "Name".. etc.]
}
I'm stuck on how to get an array of strings with the full name = ["#User Name", "#Full Name"]
Would you know how?
First of all, .filter means that check each value in the array which condition you given and if true then take value - which not fit here.
For the problem, it can divide into two task: Separate string into substring by " " ( which you have done); and combine 2 substring which starts with prefix "#"
Code will be like this
let commentString = "Hello #User Name and hello again #Full Name"
let words = commentString.components(separatedBy: " ")
let mentionQuery = "#"
var result : [String] = []
var i = 0
while i < words.count {
if words[i].hasPrefix(mentionQuery) {
result.append(words[i] + " " + words[i + 1])
i += 2
continue
}
i += 1
}
The result
print("result: ", result) // ["#User Name", "#Full Name"]
You can also use filter, like this below:
let str = "Hello #User Name and hello again #Full Name"
let res = str.components(separatedBy: " ").filter({$0.hasPrefix("#")})
print(res)
// ["#User", "#Full"]

Create a table in SPSS that counts frequencies of responses for each value of a given variable for all cases

I'm trying to convert this piece of SAS syntax to SPSS syntax, but am running into difficulties, as I'm not to familiar with how you would do this in SPSS. The basic point of the SAS syntax shown is to create an orderly table of frequencies of responses for each value of a given variable for all cases in a given dataset. I keep running into a number of issues--any ideas on how to start or on any given piece of this would be immensely helpful.
OPTIONS NOCENTER NODATE NONUMBER;
ODS RTF FILE = XRAY style=styles.test;
DATA WORK.BITS(KEEP=VCOL DCOL STUDYN MINRECL MAXRECL);
LENGTH DEFAULT=4 ;
RETAIN VCOL DCOL STUDYN MINRECL MAXRECL 0;
ARRAY PUNS (P) P12 P11 P0 P1-P9;
INFILE DATAIN N=1 RECFM=F LRECL=160 end=allgone;
VCOL=&VERCOL; DCOL=&DECKCOL;
IF ^(0 <= VCOL < 81) | ^(0 <= DCOL < 81) THEN DO;
FILE LOG;
PUT '*** INVALID COLUMN LOCATION(S) : ' VCOL= DCOL= ;
STOP;
END;
IF ^VCOL=0 & ^DCOL=0 THEN DO;
FILE LOG;
PUT '*** MULTBIN SHOULD BE USED FOR A MULTI-CARD OR MULTI-FORM XRAY ';
STOP;
END;
MAXRECL = 160;
MINRECL = 160;
if allgone then do;
STUDYN = _N_-1;
FILE LOG;
PUT '*** VCOL= ' VCOL ' / DCOL = ' DCOL ' / MAXRECL= ' MAXRECL ;
OUTPUT WORK.BITS;
end;
INPUT RECORD $CHAR160. ;
RUN;
DATA _NULL_;
IDSTRING =symget('ARCHNO');
XRAYTYPE =symget('XRAYTYPE');
OPNAME =symget('OPNAME');
FDIR = symget('dir') ;
FTYPE = symget('ftype');
SET BITS;
FILE PRINT NOTITLES PS=86 LS=132;
*put 'clear';
PUT ///;
PUT '[name] XRAY '//;
PUT 'STUDY: ' IDSTRING ;
dsfn=PATHNAME("DATAIN");
put 'Dataset: ' dsfn;
PUT 'Generated: ' "%sysfunc (today(), mmddyys10.)" ;
PUT 'Operator: ' OPNAME ;
PUT //;
PUT 'Operator provided the following information -- ';
xrfn=PATHNAME("XRAY");
put 'XRay File: ' xrfn;
PUT 'Requested Xray Type: ' XRAYTYPE ;
PUT 'Form Column: ' VCOL ;
PUT 'Card Column: ' DCOL ;
PUT //;
PUT 'Reported from the data file -- ';
PUT 'Record Length: ' MAXRECL'(max) ' MINRECL'(min) ';
PUT 'Total Records: ' STUDYN ;
PUT ////;
RUN;
*PROC PRINTTO PRINT=XRAY;
*RUN;
DATA WORK.XRAYSET (keep = column multstat idstring form card ncards varname pu1 pu2 pu3 pu4 pu5 pu6 pu7 pu8 pu9 pu0 puX puY );
INFILE DATAIN N=1 RECFM=F LRECL=160 END=ALLGONE;
FILE PRINT NOTITLES PS=86 LS=132;
LENGTH DEFAULT=4 ;
RETAIN X1-X960 CT1-CT80 CB1-CB80 CM1-CM80 NCARDS 0 ;
ARRAY XRAY (XPOS) X1-X960;
ARRAY COLTOTL(COLUMN) CT1-CT80;
ARRAY COLBLNK(COLUMN) CB1-CB80;
ARRAY COLMULT(COLUMN) CM1-CM80;
ARRAY PUNS P12 P11 P0-P9;
IF _N_=1 THEN DO;
INFILE DATAIN N=1 RECFM=F LRECL=160 END=ALLGONE ;
END;
XPOS=0; NCARDS + 1;
DO COLUMN = 1 TO 80;
INPUT #COLUMN (P12 P11 P0-P9)
(PUNCH.12 PUNCH.11 PUNCH.0 PUNCH.1 PUNCH.2 PUNCH.3
PUNCH.4 PUNCH.5 PUNCH.6 PUNCH.7 PUNCH.8 PUNCH.9) #;
CTOT=0;
DO OVER PUNS; XPOS=XPOS+1;
IF PUNS>0 THEN DO;
CTOT=CTOT+1;
XRAY=XRAY+1;
END;
END;
IF CTOT=0 THEN COLBLNK=COLBLNK+1;
ELSE DO;
IF CTOT>1 THEN COLMULT=COLMULT+1;
COLTOTL=COLTOTL+CTOT;
END;
END;
/*IGNORE:*/
IF ALLGONE THEN DO; * FINAL SUMMARY;
IDSTRING=symget('ARCHNO');
XRAYTYPE=symget('XRAYTYPE');
FORM = '1'; CARD = '1'; DCOL = '0';
/* IDSTRING=STUDYID; INPUT STUDYID */
FILE PRINT NOTITLES PS=86 LS=132;
PUT #1 ' Column Frequencies for ' IDSTRING ' TYPE=' XRAYTYPE ' FORM ' FORM ' CARD ' CARD '(COL=' DCOL')' ;
PUT ' Source: The [name], ' "%sysfunc(today(), mmddyys10.)" ' Records = ' NCARDS /;
PUT ' COL.# 12& 11- 0 1 2 3 4 5 6 '
' 7 8 9 BLNK MULT SUM COL.#' / ;
PUT OVERPRINT
' _____ _____ _____ _____ _____ _____ _____'
' _____ _____ _____ _____ _____ _____ _____'
' _____ ______ ____' ;
XPOS=0;
length valid $2000.;
length puY puX pu0 - pu9 $5.;
length varname $4.;
DO COLUMN=1 TO 80;
PUT COLUMN 6. #;
DO CTOT=1 TO 12; XPOS=XPOS+1;
PUT XRAY 6. # ;
*if colmult > 0 then do;
if xray > 0 then do;
valid = trim(valid)||' P'||left(put(ctot,2.));
if ctot = 1 then puY='*';
if ctot = 2 then puX='*';
if ctot = 3 then pu0='*';
if ctot = 4 then pu1='*';
if ctot = 5 then pu2='*';
if ctot = 6 then pu3='*';
if ctot = 7 then pu4='*';
if ctot = 8 then pu5='*';
if ctot = 9 then pu6='*';
if ctot = 10 then pu7='*';
if ctot = 11 then pu8='*';
if ctot = 12 then pu9='*';
end;
*end;
END;
PUT (COLBLNK COLMULT COLTOTL COLUMN)(6.);
multstat = colmult;
valpun = valid;
OUTPUT ;
valid='';
puY='';puX='';pu0='';pu1='';pu2='';pu3='';pu4='';pu5='';pu6='';pu7='';pu8='';pu9='';
END;
END;
RETURN;
RUN;
*PROC PRINTTO;
*RUN;
ODS RTF CLOSE;
Here's an example pic of what this SAS syntax can generate

Ruby on Rails 4 - sort records in .each and create records

In my application, I have 2 models Post & PostTexts.
Each Post has language, position, title & description.
Positions are: Title & Description.
Short about my application to get a better understanding. User comes in and selects languages & texts that they want and will get a result of different post texts that they can use.
For instance user selects:
Title 1 => position: Title
Title 2 => position: Title
Description 1 => position: Description
Languages: English, Spanish
Currently I'm creating the texts by:
#languages = params[:languages].split(',')
#post_texts = params[:post_texts].split(',')
#post_texts.split(',').each do |id|
#languages.split(',').each do |language|
post_text = PostText.where(post_text_id: id)
#titles = post_text.where(placement: 'title')
#descriptions = banner_text.where(placement: 'description')
end
end
#titles.each do |title|
#descriptions.each do |description|
Post.create(
title: title.body,
description: description.body,
language: 'language here',
)
end
end
This works fine if I only select 1 language and result would be as:
Title 1 + Description 1
Title 2 + Description 1
But when I select several languages, texts mixes up and Spanish texts mixes up with english and vice versa.
What I want to achieve is when several languages has been selected, only English texts go with English and Spanish texts go with Spanish etc.
so result would be as:
English Title 1 + English Description 1
English Title 2 + English Description 1
Spanish Title 1 + Spanish Description 1
Spanish Title 2 + Spanish Description 1
Current result is something as below:
English Title 1 + English Description 1
English Title 1 + Spanish Description 1
Spanish Title 2 + Spanish Description 1
Spanish Title 2 + English Description 1
ps: Sorry if the description is not that good
I have also tested with 2 different ways but results are the same:
1
#languages = params[:languages].split(',')
#post_texts = params[:post_texts].split(',')
#post_texts.split(',').each do |id|
post_text = PostText.where(post_text_id: id)
#titles = post_text.where(placement: 'title')
#descriptions = banner_text.where(placement: 'description')
end
#languages.split(',').sort { |a,b| a.lang <=> b.lang }.each do |lang|
#bodies = #titles.where(language: lang)
#ctas = #descriptions.where(language: lang)
end
#titles.each do |title|
#descriptions.each do |description|
Post.create(
title: title.body,
description: description.body,
language: 'language here',
)
end
end
2
#languages = params[:languages].split(',')
#post_texts = params[:post_texts].split(',')
#post_texts.split(',').each do |id|
post_text = PostText.where(post_text_id: id)
#titles = post_text.where(placement: 'title')
#descriptions = banner_text.where(placement: 'description')
end
#languages.split(',').map { |lang|
#bodies = #titles.where(language: lang)
#ctas = #descriptions.where(language: lang)
end
#titles.each do |title|
#descriptions.each do |description|
Post.create(
title: title.body,
description: description.body,
language: 'language here',
)
end
end
Any help is appreciated and thanks in advance!
In order to get the results you want, you need to specify it in your last loop, where you create your records, by doing .where(field: loop.value), which in your case would be where(language: title.language) and the same with your post_text variable.
Shange
post_text = PostText.where(post_text_id: id)
To:
post_text = PostText.where(post_text_id: id).where(language: lang)
The code would be as below:
#languages = params[:languages].split(',')
#post_texts = params[:post_texts].split(',')
#post_texts.split(',').each do |id|
#languages.split(',').each do |lang|
post_text = PostText.where(post_text_id: id).where(language: lang)
#titles = post_text.where(placement: 'title')
#descriptions = banner_text.where(placement: 'description')
end
end
#titles.each do |title|
#descriptions.where(language: title.language).each do |description|
Post.create(
title: title.body,
description: description.body,
language: 'language here',
)
end
end
The main part that has been changed is #descriptions.each do |description|, by changing it to #descriptions.each do |description|

LUA getting values from table by using a string

How would I go about compiling values from a table using a string?
i.e.
NumberDef = {
[1] = 1,
[2] = 2,
[3] = 3
}
TextDef = {
["a"] = 1,
["b"] = 2,
["c"] = 3
}
If I was for example to request "1ABC3", how would I get it to output 1 1 2 3 3?
Greatly appreciate any response.
Try this:
s="1ABC3z9"
t=s:gsub(".",function (x)
local y=tonumber(x)
if y~=nil then
y=NumberDef[y]
else
y=TextDef[x:lower()]
end
return (y or x).." "
end)
print(t)
This may be simplified if you combine the two tables into one.
You can access values in a lua array like so:
TableName["IndexNameOrNumber"]
Using your example:
NumberDef = {
[1] = 1,
[2] = 2,
[3] = 3
}
TextDef = {
["a"] = 1,
["b"] = 2,
["c"] = 3
}
print(NumberDef[2])--Will print 2
print(TextDef["c"])--will print 3
If you wish to access all values of a Lua array you can loop through all values like so (similarly to a foreach in c#):
for i,v in next, TextDef do
print(i, v)
end
--Output:
--c 3
--a 1
--b 2
So to answer your request, you would request those values like so:
print(NumberDef[1], TextDef["a"], TextDef["b"], TextDef["c"], NumberDef[3])--Will print 1 1 2 3 3
One more point, if you're interested in concatenating lua string this can be accomplished like so:
string1 = string2 .. string3
Example:
local StringValue1 = "I"
local StringValue2 = "Love"
local StringValue3 = StringValue1 .. " " .. StringValue2 .. " Memes!"
print(StringValue3) -- Will print "I Love Memes!"
UPDATE
I whipped up a quick example code you could use to handle what you're looking for. This will go through the inputted string and check each of the two tables if the value you requested exists. If it does it will add it onto a string value and print at the end the final product.
local StringInput = "1abc3" -- Your request to find
local CombineString = "" --To combine the request
NumberDef = {
[1] = 1,
[2] = 2,
[3] = 3
}
TextDef = {
["a"] = 1,
["b"] = 2,
["c"] = 3
}
for i=1, #StringInput do --Foreach character in the string inputted do this
local CurrentCharacter = StringInput:sub(i,i); --get the current character from the loop position
local Num = tonumber(CurrentCharacter)--If possible convert to number
if TextDef[CurrentCharacter] then--if it exists in text def array then combine it
CombineString = CombineString .. TextDef[CurrentCharacter]
end
if NumberDef[Num] then --if it exists in number def array then combine it
CombineString = CombineString .. NumberDef[Num]
end
end
print("Combined: ", CombineString) --print the final product.

vbscript array loop, printing issue

I have a recordset like this:
row1 = Type1, partid, partname
row2 = Type2, partid, partname
row3 = Type2, partid, partname
I want my output to be like this:
Type1: partid partname
Type2: partid partname, partid, partname
or
Type2: partid partname, partid, partname
Type1: partid partname
(depending on how the sort works out)
I have my results sorted by the Type value. I may or may not get same Types in a result, but if so the result is required to be printed as above.
I'm using .getRows() to get the recordset into an array.
I can't for the life of me figure out how to decide in the loop whether to print a break tag or a comma.
You need to check for a change in the type column. In that case, print <br /> and the type. In code:
Set r = CreateObject("ADODB.Recordset")
r.Fields.Append "t", 3
r.Fields.Append "i", 3
r.Fields.Append "n", 3
r.Open
r.AddNew : r.Fields("t") = 1 : r.Fields("i") = 10 : r.Fields("n") = 10
r.AddNew : r.Fields("t") = 2 : r.Fields("i") = 20 : r.Fields("n") = 21
r.AddNew : r.Fields("t") = 2 : r.Fields("i") = 22 : r.Fields("n") = 22
r.AddNew : r.Fields("t") = 3 : r.Fields("i") = 30 : r.Fields("n") = 31
r.MoveFirst
a = r.GetRows()
t = a(0, 0)
WScript.StdOut.Write a(0, 0)
For r = 0 To UBound(a, 2)
If t <> a(0, r) Then
WScript.StdOut.Write "<br />" & a(0, r)
t = a(0, r)
End If
WScript.StdOut.Write Join(Array("", a(1, r), a(2,r)), ",")
Next
output:
cscript 24441584.vbs
1,10,10<br />2,20,21,22,22<br />3,30,31

Resources