So, I want to insert a number/data in the list but it invalid syntax.I want to make a system that accepts number and input it to the list then I want it to be edited or modified if I want to.
Here's my code for the modifying or editing:
list1 = []
edit = int(input('Enter 1 to edit grade for math, 2 for science, 3 for english: '))
if edit == 1:
grade1 = int(input('Enter grade:')
list1.insert(0,grade1)
elif edit == 2:
grade2 = int(input('Enter grade:')
list1.insert(1,grade2)
grade1 = int(input('Enter grade:')
elif edit == 3:
grade3 = int(input('Enter grade:')
list1.insert(2,grade3)
else:
print('error')
print(adds)
This should work -
list1 = []
edit = int(input('Enter 1 to edit grade for math, 2 for science, 3 for english: '))
if edit == 1:
grade1 = int(input('Enter grade:'))
list1.insert(0,grade1)
elif edit == 2:
grade2 = int(input('Enter grade:'))
list1.insert(1,grade2)
grade1 = int(input('Enter grade:'))
elif edit == 3:
grade3 = int(input('Enter grade:'))
list1.insert(2,grade3)
else:
print('error')
print(adds)
Formatting is important in Python. The proper formatted Python code is as below :
list1 = []
edit = int(input('Enter 1 to edit grade for math, 2 for science, 3 for english: '))
if edit == 1:
grade1 = int(input('Enter grade:')
list1.insert(0,grade1)
elif edit == 2:
grade2 = int(input('Enter grade:')
list1.insert(1,grade2)
grade1 = int(input('Enter grade:')
elif edit == 3:
grade3 = int(input('Enter grade:')
list1.insert(2,grade3)
else:
print('error')
print(adds)
This however will fail as :
i) The closing bracket of none of the int calls is in place.
ii) For any initial input apart from 1,2,3, the code will fail as 'adds' is not defined.
Related
I know how to make a bar graph but I'm trying to put a append two append lists inside of the graph
one is the quantity of a item and the other is the item I would like to know if this is possible and if it is how should I do this. This is what I tried.
itemls = [0]
itemquantityls = [0]
option = 0
while option < 3:
Mainmenue = ["Main menue",
"1. ADD AN ITEM",
"2. GENERATE A CHART",
"3. EXIT"]
for i in (Mainmenue):
print(i)
option = int(input("Option you want:"))
if option == 1:
item = (input("add an item:"))
itemquantity = (input("Item quantity:"))
itemls.append(item)
itemquantityls.append(itemquantity)
elif option == 2:
import matplotlib.pyplot as plt
plt.bar(itemls, itemquantityls)
plt.title('Items and quantity')
plt.xlabel('Item')
plt.ylabel('Item quantity')
plt.show()
elif option == 3:
print("program is teminating")
else:
print("Wrong Input try again")
Thanks in advance. :)
I'm using this code:
ovabonnement <- ovabonnement %>%
mutate(c12_ovabonnement_type_con_voor = case_when(s2_ovabonnement_type_voor_anders == 1 ~ NA,
s2_ovabonnement_type_voor_1 == 1 |
s2_ovabonnement_type_voor_13 == 1 ~ "Basis",
s2_ovabonnement_type_voor_2 == 1 |
s2_ovabonnement_type_voor_3 == 1 |
s2_ovabonnement_type_voor_4 == 1 |
s2_ovabonnement_type_voor_9 == 1 |
s2_ovabonnement_type_voor_11 == 1 ~ "Voordeel",
s2_ovabonnement_type_voor_5 == 1 |
s2_ovabonnement_type_voor_6 == 1 |
s2_ovabonnement_type_voor_7 == 1 |
s2_ovabonnement_type_voor_8 == 1 |
s2_ovabonnement_type_voor_10 == 1 |
s2_ovabonnement_type_voor_12 == 1 |
s2_ovabonnement_type_voor_14 == 1 ~ "Vrij"))
So I have these 15 variables that represent whether a person has that subscription added onto their public transport membership. Because it was a multiple choice questionnaire people could select multiple choices, which is why they are different variables.
I want to make these into one variable that takes NA if people answered "other", "Basis" if people answered 1 or 13, "Voordeel" if people answered 2,3,4,9 or 11 and "Vrij" if people answered 5,6,7,8,10,12 or 14.
If people answered 2, there will be a 1 in s2_ovabonnement_type_voor_2. People can have answered multiple of these, which makes it a bit tricky. However, I want it to go through these chronologically. For example, if a person answered 2 AND 10, it should choose the 10, because the code is later, but I'm not sure if that is how case_when works.
I get this error:
Error in `mutate()`:
! Problem while computing `c12_ovabonnement_type_con_voor = case_when(...)`.
Caused by error in `names(message) <- `*vtmp*``:
! 'names' attribute [1] must be the same length as the vector [0]
Run `rlang::last_error()` to see where the error occurred.
case_when/if_else are type sensitive i.e all the expressions should return the same type. In the OP's expression, the first expression returns NA and NA by default is logical, and all others return character type. We need NA_character_ to match the type of others
ovabonnement <- ovabonnement %>%
mutate(c12_ovabonnement_type_con_voor = case_when(s2_ovabonnement_type_voor_anders == 1 ~ NA_character_,
s2_ovabonnement_type_voor_1 == 1 |
s2_ovabonnement_type_voor_13 == 1 ~ "Basis",
s2_ovabonnement_type_voor_2 == 1 |
s2_ovabonnement_type_voor_3 == 1 |
s2_ovabonnement_type_voor_4 == 1 |
s2_ovabonnement_type_voor_9 == 1 |
s2_ovabonnement_type_voor_11 == 1 ~ "Voordeel",
s2_ovabonnement_type_voor_5 == 1 |
s2_ovabonnement_type_voor_6 == 1 |
s2_ovabonnement_type_voor_7 == 1 |
s2_ovabonnement_type_voor_8 == 1 |
s2_ovabonnement_type_voor_10 == 1 |
s2_ovabonnement_type_voor_12 == 1 |
s2_ovabonnement_type_voor_14 == 1 ~ "Vrij"))
Sample code snippet tried:
for row in range(1,sheet.max_row+1):
for col in range(1, sheet.max_column+1):
temp = None
cell_obj = sheet.cell(row=row,column=col)
temp = re.search(r"requestor", str(cell_obj.value))
if temp:
if 'requestor' in cell_obj.value:
cell_obj.value.replace('requestor',
'ABC')
Trying to replace from an xlsx cell containing value "Customer name: requestor " with value "Customer name: ABC" .How can this be achieved easily ?
I found my answer in this post:https://www.edureka.co/community/42935/python-string-replace-not-working
The replace function doesn't store the result in the same variable. Hence the solution for above:
mvar = None
for row in range(1,sheet.max_row+1):
for col in range(1, sheet.max_column+1):
temp = None
cell_obj = sheet.cell(row=row,column=col)
temp = re.search(r"requestor", str(cell_obj.value))
if temp:
if 'requestor' in cell_obj.value:
mvar = cell_obj.value.replace('requestor',
'ABC')
cell_obj.value = mvar
Just keep it simple. Instead of re and replace, search for the given value and override the cell.
The example below also gives you the ability to change 'customer name' if needed:
wb = openpyxl.load_workbook("example.xlsx")
sheet = wb["Sheet1"]
customer_name = "requestor"
replace_with = "ABC"
search_string = f"Customer name: {customer_name}"
replace_string = f"Customer name: {replace_with}"
for row in range(1, sheet.max_row + 1):
for col in range(1, sheet.max_column + 1):
cell_obj = sheet.cell(row=row, column=col)
if cell_obj.value == search_string:
cell_obj.value = replace_string
wb.save("example_copy.xlsx") # remember that you need to save the results to the file
I am strugling with this loop. I want to get "6" in the second row of column "Newcolumn".I get the following error.
Error in if (mydata$type_name[i] == "a" && mydata$type_name[i - :
missing value where TRUE/FALSE needed.
The code that I created:
id type_name name score newcolumn
1 a Car 2 2
1 a van 2 6
1 b Car 2 2
1 b Car 2 2
mydata$newcolumn <-c(0)
for (i in 1:length(mydata$id)){
if ((mydata$type_name [i] == "a") && (mydata$type_name[i-1] == "a") && ((mydata$name[i]) != (mydata$name[i-1]))){
mydata$newcolumn[i]=mydata$score[i]*3 }
else {
mydata$newcolumn[i]=mydata$score[i]*1
}
}
Thank you very much in advance
List starts at index 1 in R but like you are doing a i-1 in your loop starting at 1, your list is out of range (i-1=0) so your code can not return a True or False.
Good day. I am a newbie in creating a python program. My program is to write code that prints Hello if 1 is stored in spam, prints Howdy if 2 is stored in spam, and prints Greetings! if anything else is stored in spam.
My problem is that I would like to repeat the process if the user will not input anything or an empty value. How will I convert '' to value 0. Thanks. Im sorry if my program is not that good.
while True:
print ('Enter value of spam')
spam = int(input())
if spam == 1:
print ('Hello')
continue
elif spam == 2:
print ('Howdy')
continue
elif spam != 0:
print ('Greeting')
continue
Try this.
print ('Enter value of spam')
spam = input()
while spam != "" or spam == 0:
if int(spam) == 1:
print ('Hello')
elif int(spam)== 2:
print ('Howdy')
elif int(spam)!= 0:
print ('Greeting')
print ('Enter value of spam')
spam = input()
You could use a try-except block to handle any situation where your user's input can't be understood as an integer:
while True:
print ('Enter value of spam')
try:
spam = int(input())
except ValueError:
spam = 0
if spam == 1:
print ('Hello')
continue
elif spam == 2:
print ('Howdy')
continue
elif spam != 0:
print ('Greeting')
continue