+ 1

What is Wrong? Lang == R

https://code.sololearn.com/cPwl233BYTRV/?ref=app my code : CA <-"California" NY <- "New York" FL <- "Florida" OH <- "Ohio" state <- readLines('stdin') #your code goes here if (state==CA){ print("California") if (state==NY){ print("New york") if (state==FL){ print("Florida") if (state==OH){ print("Ohio") } } } } Please help me!

5th Jun 2022, 9:06 AM
Hrithika Reddy
Hrithika Reddy - avatar
11 Respuestas
0
Hrithika Reddy You have a number of variables which contain values. Example: CA <- "California" Then you have an input which will receive a string. But when you start your if statements, you are looking for your input to produce a variable, which will never happen. Your input produces a string. I said that twice because it is important. So if you look for a matching string, you can print your variable, which will produce the value. Example attached for you to review: CA <-"California" NY <- "New York" FL <- "Florida" OH <- "Ohio" state <- readLines('stdin') #your code goes here if (state== "CA"){ print(CA) } if (state=="NY"){ print(NY) } if (state=="FL"){ print(FL) } if (state=="OH"){ print(OH) }
5th Jun 2022, 10:35 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Nested means that you're putting conditions within another condition. I could tell off the bat because you have all the ending brackets at the very end. So if you had indentation, your code would look like if (statement) { #code if (statement) { #code if (statement) { #code if (statement) { #code } } } } This is also why proper indentation is important for readability. Not just for others to read, but you yourself. So because if I as the user enter Ohio, it does not matter, because I have not passed the conditions for the 3 previous states. You need to seperate all the statements to make it look like... if (statement) { #code } if (statement) { #code } if (statement) { #code } if (statement) { #code }
5th Jun 2022, 9:18 AM
Justice
Justice - avatar
+ 2
In case this is 11.2 of the sololearn R course – the purpose if the task is to demonstrate that you understand the switch-statement. You can re-view lesson 11.1 for it.
5th Jun 2022, 10:43 AM
Lisa
Lisa - avatar
+ 1
I'm not familiar with R at all, but are you sure about nesting all those if statements? Just going off programming logic since I don't know the language, by nesting the then if statements, you're not going to be able to check the nested ifs. You're going to want to seperate them unless they'll never pass the condition to be able to get to them in the first place.
5th Jun 2022, 9:11 AM
Justice
Justice - avatar
+ 1
What do you expect as input? Should we input CA or California?
5th Jun 2022, 10:09 AM
Lisa
Lisa - avatar
+ 1
Thanks rik !
5th Jun 2022, 10:41 AM
Hrithika Reddy
Hrithika Reddy - avatar
+ 1
Its ok Rik has helpled me solve it and yes and if you input CA it will output calfornia
5th Jun 2022, 10:45 AM
Hrithika Reddy
Hrithika Reddy - avatar
+ 1
Hrithika Reddy Lisa has made a good point, it is worthy to review the concepts if you are required to use a particular method. Most codes can be resolved using different systems, & practice can only make you a better coder
5th Jun 2022, 10:57 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Thx , i aldready re-checked and saw my mistake Rik Wittkopp
5th Jun 2022, 10:58 AM
Hrithika Reddy
Hrithika Reddy - avatar
0
What is nested is? I dont know that??
5th Jun 2022, 9:11 AM
Hrithika Reddy
Hrithika Reddy - avatar
0
Hrithika Reddy PS: Have a look at the code examples given to you regarding their readability. Good code layout makes debugging so much easier for everyone
5th Jun 2022, 10:38 AM
Rik Wittkopp
Rik Wittkopp - avatar