new python programmer, looking for a fix for this issue. I'm making an autofill rubric for a school project, how would I move to a different section of my code. For example, I want to be able to re-run this function, but that breaks my input because I cannot put it into the function itself.
#TODO...
#Add students names in IN the code, same with num of students
numberOfStudents = 4
rubrick = ["Preparedness", "Engagement", "Perseverance", "Problem Solving", "Progessionalism"]
students = ["ROBERT", "DEVIN", "SKYLER", "XAVIER"]
def pickStudent():
print('Please select your student...')
for x in range(len(students)):
print(students[x])
pickStudent()
userPicked = input().upper()
if userPicked == students[0]:
print("You picked... " + students[0])
elif userPicked == students [1]:
print("You picked... " + students[1])
elif userPicked == students [2]:
print("You picked... " + students[2])
elif userPicked == students [3]:
print("You picked... " + students[3])
else:
print("Invalid user... \n")
pickStudent()
userPicked = input().upper()
Any help would be apreciated.
A few things to pick out here.
You probably want to put the user picking in a while loop to ensure it keeps asking until a correct answer is given.
You also don't need to test the entry against each item in the list in turn - instead use the in operator to see if the inputted text is found ion the list.
Combining these gives you:
while userPicked not in students:
userPicked = input().upper()
Instead of looping over students to print them, you can write neater code using join - to join all the items in the students list into a string and print that:
print('\n'.join(students))
Related
I'm using gtsummary::tbl_regression to format the output of the following code:
dat.mi <- import(here("temp", "adTurn_mi.rds"))
fit1 <- glm(quit ~ sex + age + race + education + experience + tenure_sr +
salary + anymc + catcap + medi + nonpro + rural,
family = 'binomial',
weights = svy_weight,
data = complete(dat.mi)
) |>
tbl_regression(exponentiate = TRUE)
dat.mi is MICE data (unfortunately I can't provide a truly reproducible example as the data is confidential), but is completed in the call to the glm() function. tbl_regression formats everything beautifully, with the exception of one line, and I cannot figure out what that one line is doing or how to fix it. Instead of returning an exponentiated coeficient, it returns a string of numbers separated by commas (please see the Masters degree line in the attached image). Is this a known issue with a similarly known and easy fix? Or am I just really good at breaking stuff?
I am trying to write a routine that counts the characters in a global.
These are the globals I set and the characters I would like counted.
s ^XA(1)="SYLVESTER STALLONE, BRUCE WILLIS, AND ARNOLD SCHWARZENEGGER WERE DISCUSSING THEIR "
s ^XA(2)="NEXT PROJECT, A BUDDY FILM IN WHICH BAROQUE COMPOSERS TEAM UP TO BATTLE BOX-OFFICE IRRELEVANCE "
s ^XA(3)="EVERY HAD BEEN SETTLED EXCEPT THE CASTING. "
s ^XA(4)="""ARNOLD CAN BE PACHELBEL,"" STALLONE. ""AND I WANT TO PLAY MOZART. """
s ^XA(5)="""NO WAY!"" SAID WILLIS. ""YOU'RE NOT REMOTELY MOZARTISH. """
s ^XA(6)="""I'LL PLAY MOZART. YOU CAN BE HANDEL. """
s ^XA(7)="""YOU BE HANDEL!"" YELLED STALONE. ""I'M PLAYING MOZART! """
s ^XA(8)="FINALLY, ARNOLD SPOKE ""YOU WILL PLAY HANDEL,"" HE SAID TO WILLIS. "
s ^XA(9)="""AND YOU,"" HE SAID TO STALLONE, ""THEN WHO ARE YOU GONNA PLAY? """
s ^XA(10)="""OH YEAH?"" SAID STALLONE, ""THEN WHO ARE YOU GONNA PLAY? """
s ^XA(11)="ARNOLD ROSE FROM THE TABLE AND DONNED A PAIR OF SUNGLASSES. "
s ^XA(12)="I'LL BE MOZART."
If I understood your question correctly, and you just need the total count of all characters in a global, here you go:
set key = ""
for {
set key = $Order(^XA(key))
quit:key=""
for i=1:1:$Length(^XA(key)) {
set char = $Extract(^XA(key), i)
set count(char) = $get(count(char)) + 1
}
}
zwrite count // or just return count
As for your example, this will produce the following output:
count(" ")=112
count("!")=3
count("""")=24
count("'")=4
count(",")=9
count("-")=1
count(".")=11
count("?")=3
count("A")=54
count("B")=12
count("C")=13
count("D")=23
count("E")=60
count("F")=6
count("G")=8
count("H")=20
count("I")=28
count("J")=1
count("K")=1
count("L")=48
count("M")=11
count("N")=39
count("O")=44
count("P")=13
count("Q")=1
count("R")=28
count("S")=29
count("T")=33
count("U")=13
count("V")=3
count("W")=11
count("X")=3
count("Y")=21
count("Z")=6
Hope this helps!
I'm just getting started with coding and took python as my first language. I decided to do this guessing number mini-project and I've gotten this far. It works perfectly but I want the user to only input a number between 1-10 and if it exceeds that or other input is given that isn't in that range. I want to print out a text.
I have been scouring the python documentation and haven't found anything, I'm pretty sure it's something pretty simple in the conditions but I can't figure out what it is.
Also if you see any way that this code could be improved please tell me I would love to know it
def game():
mysteryNumber = random.randint(1,10)
print("I just guessed a number.")
inputByUser = input("Now choose a number from 1 to 10 : ")
chosenNumber = int(inputByUser)
if mysteryNumber == chosenNumber:
print("You guessed it right.")
elif mysteryNumber > chosenNumber:
print("Too low. Try again buddy.")
elif mysteryNumber < chosenNumber:
print("Too high. Try again buddy.")
else:
print("The number you chose ' {} ' is not a valid number.".format(chosenNumber))
Here is a picture of the full code
EDIT: Nevermind I figured it myself I added this line of code after the first if statement
elif chosenNumber > 10:
print("The number you chose ' {} ' is not a valid number.".format(chosenNumber))
game()
It's not a perfect solution as it doesn't check within the range but I'll learn more about that down the road
You could improve your if statement
elif (chosenNumber > 10) or (chosenNumber < 1):
print("The number you chose ' {} ' is not a valid number.".format(chosenNumber))
game()
I use this code to show the sum of my bank
if {?Bank} = {Bank.sum} then {#1} else 0
I have 6 bank accounts and when the bank = Bank has operations, then I show the sumand else I do not show anything.
I do not want to show "0" in my report.
I want to show " "(space), but when I change it to " ".
When I do it, I get the error
A number is required here
How can I fix it?
In that case, use:
if {?Bank} = {Bank.sum} then ToText({#1},0) else ""
The 2nd arg indicates zero decimal points. See online documentation of the ToText() function.
One option:
if {?Bank} = {Bank.sum} then ToText({#1}) else ""
Another option is to use formatting.
I am new in Swift.
I am trying to make a budget application. This app have a Calculator like keyboard. My idea is when users enter the money app will automatically add a decimal place for users.
For example, if you type 1230 it will give you 12.30 and type 123 it will display 1.23
I wrote a couple lines of code down below. The problem is it only can add decimal point after first digit it won't go backwards when you give more digits. It only can display as X.XXXXX
I tried solve this problem with String.index(maybe increase index?) and NSNumber/NSString format. But I don't know this is the right direction or not.
let number = sender.currentTitle!
let i: String = displayPayment.text!
if (displayPayment.text?.contains("."))!{
displayPayment.text = i == "0" ? number : displayPayment.text! + number
}
else {
displayPayment.text = i == "0" ? number : displayPayment.text! + "." + number
}
Indexing Strings in Swift is not as "straightforward" as many would like, simply due to how Strings are represented internally. If you just want to add a . at before the second to last position of the user input you could do it like this:
let amount = "1230"
var result = amount
if amount.characters.count >= 2 {
let index = amount.index(amount.endIndex, offsetBy: -2)
result = amount[amount.startIndex..<index] + "." + amount[index..<amount.endIndex]
} else {
result = "0.0\(amount)"
}
So for the input of 1230 result will be 12.30. Now You might want to adjust this depending on your specific needs. For example, if the user inputs 30 this code would result in .30 (this might or might not be what you want).