How do i rewrite this python code in c#? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i rewrite this python code in c#?

if 0 < (guess-x) > 5 : print("Wrong, the number is lower") elif 0 < (x-guess) > 5 : print("Wrong,the number is higher") elif 0 < (guess-x) <= 5 : print('wrong but very close, the number is lower') elif 0 < (x-guess) <= 5 : print('wrong but very close, the number is higer') else: print("Correct guess")

2nd Feb 2022, 11:17 AM
Lenoname
2 Answers
+ 3
if 0 < (guess-x) > 5 : #syntax works only python, This means if( (0< guess-x) && (guess-x >5) ) a compound conditional statement.. this way equals in almost other languages c, c++ , c#, java.
2nd Feb 2022, 11:47 AM
Jayakrishna 🇮🇳
+ 1
Remember the DRY principle - Don't Repeat Yourself. Calculate the difference once, then use that in the comparisons. Also avoid repeating comparisons. Here is reduced logic that translates more easily into C#: diff = guess - x if diff>5: print("Wrong, the number is lower") elif diff<-5: print("Wrong, the number is higher") elif diff>0: print('Wrong but very close, the number is lower') elif diff<0: print('Wrong but very close, the number is higher') else: print("Correct guess") To translate into C# you must: - Declare variable types. - Terminate statements with semicolon. - Put parentheses around conditionals. - Remove separating colons. - Use Console.WriteLine instead of print. - Replace elif with else if. - Use only " for strings, not '.
3rd Feb 2022, 7:38 AM
Brian
Brian - avatar